This method replaces zero or more format specifications with the appropriately formatted value of an object.
[Visual Basic] Overloads Public Function AppendFormat( _ ByVal format As String, _ ByVal args() As Object, _ ByVal sop As IServiceObjectProvider _ ) As StringBuilder [C#] public StringBuilder AppendFormat( string format, object[] args, IServiceObjectProvider sop ); [C++] public: StringBuilder* AppendFormat( String* format, Object* args[], IServiceObjectProvider* sop ); [JScript] public function AppendFormat( format : String, args : Object[], sop : IServiceObjectProvider ) : StringBuilder;
A StringBuilder containing a copy of format
after replacing any format specifications with the appropriately formatted value.
Exception Type | Condition |
---|---|
ArgumentNullException | if any of the object parameters is a null reference (in Visual Basic Nothing). |
FormatException | if format is invalid. |
The format specifications are embedded within a string and are of the form {N:FormatString}, where N is a zero-based number indicating the argument that is to be formatted and FormatString is a format string . If args[N] implements IFormattable then args[N].Format (format, a null reference (Nothing)) provides the formatting; otherwise args[N].ToString() provides the formatting. If args[N] is a null reference (Nothing) then an empty string is used in its place.
The sop parameter specifies the format provider from which the NumberFormatInfo instance will be obtained. NumberFormatInfo supplies information such as the characters to be used for decimal and group separators, and determines the spelling and placement of currency symbols in monetary values. If sop is a null reference (Nothing), the numeric format information is obtained from the current locale.
To Pad With Spaces: It is possible to define a region within the string argument in which the formatted value can be left or right justified. The format specification has an optional integer field: {N[,M]:FormatString}. The magnitude of M indicates the minimum number of spaces for the region and the sign of M indicates whether the value should be left (negative values) or right (positive values) justified within the region.
ToString is Used if the Format String is Omitted: The format string within a format specification is optional: {N[,M][:FormatString]}. When FormatString is omitted, the ToString method of the argument specified by N is used to replace the format specification. If M is given, then the result of ToString is padded with spaces as described above.
If the string assigned to format is "Thank you for your purchase of {0:####} copies of Better Basket Weaving." and arg[0] is an Int16 with the value 123, then the return value will be "Thank you for your purchase of 123 copies of Better Basket Weaving."
If the string assigned to format is "Brad's dog has {0,-8:G} fleas." and arg[0] is an Int16 with the value 42, then the return value will be "Brad's dog has 42 fleas."
StringBuilder Class | StringBuilder Members | System.Text Namespace | StringBuilder.AppendFormat Overload List