Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Each format item is replaced by the string representation of a corresponding argument in a parameter array using a specified format provider.
A reference to this instance after the append operation has completed. After the append operation, this instance contains any data that existed before the operation, suffixed by a copy of format where any format specification is replaced by the string representation of the corresponding object argument.
Type Reason ArgumentNullException format or args is a null reference. FormatException format is invalid.
This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current System.Text.StringBuilder object.
The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to objects in the parameter list of this method. The formatting process replaces each format item with the string representation of the corresponding object.
The syntax of a format item is as follows:
{index[,length][:formatString]}
Elements in square brackets are optional. The following table describes each element.
index |
The zero-based position in the parameter list of the object to be formatted. If the object specified by index is null, the format item is replaced by string.Empty. If there is no parameter in the index position, a FormatException is thrown. |
,length |
The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:formatString |
A standard or custom format string that is supported by the parameter. |
For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.
The provider parameter specifies an IFormatProvider implementation that can provide formatting information for the objects in args. provider can be any of the following:
A System.Globalization.CultureInfo object that provides culture-specific formatting information.
A System.Globalization.NumberFormatInfo object that provides culture-specific formatting information for numeric values in args.
A System.Globalization.DateTimeFormatInfo object that provides culture-specific formatting information for date and time values in args.
A custom IFormatProvider implementation that provides formatting information for one or more of the objects in args. Typically, such an implementation also implements the ICustomFormatter interface. The second example in the next section illustrates an StringBuilder.AppendFormat(IFormatProvider, string, Object[]) method call with a custom IFormatProvider implementation.
If the provider parameter is null, format provider information is obtained from the current culture.
args represents the objects to be formatted. Each format item in format is replaced with the string representation of the corresponding object in args. If the format item includes formatString and the corresponding object in args implements the IFormattable interface, then args[index].Format(formatString, provider) defines the formatting. Otherwise, args[index].ToString(provider) defines the formatting.
C# Example
using System; using System.Text; public class StringBuilderTest { public static void Main() { string a = "very"; string b = "very"; string c = "high"; StringBuilder sb = new StringBuilder("The high "); Console.WriteLine(sb.AppendFormat(null, "temperature today was {0}, {1} {2}.", a, b, c) ); } }
The output is
The high temperature today was very, very high.