StringBuilder::AppendFormat Method (String^, Object^)
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 single argument.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- format
-
Type:
System::String^
A composite format string (see Remarks).
- arg0
-
Type:
System::Object^
An object to format.
Return Value
Type: System.Text::StringBuilder^A reference to this instance with format appended. Each format item in format is replaced by the string representation of arg0.
| Exception | Condition |
|---|---|
| ArgumentNullException | format is null. |
| FormatException | format is invalid. -or- The index of a format item is less than 0 (zero), or greater than or equal to 1. |
| ArgumentOutOfRangeException | The length of the expanded string would exceed MaxCapacity. |
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 StringBuilder object.
The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items. The index of the format items must be 0, to correspond to arg0, the single object in the parameter list of this method. The formatting process replaces each format item with the string representation of arg0.
The syntax of a format item is as follows:
{index[,length][:formatString]}
Elements in square brackets are optional. The following table describes each element.
Element | Description |
|---|---|
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. |
Note |
|---|
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. |
arg0 represents the object to be formatted. Each format item in format is replaced with the string representation of arg0. If the format item includes formatString and arg0 implements the IFormattable interface, then arg0.ToString(formatString, null) defines the formatting. Otherwise, arg0.ToString() defines the formatting.
If the string assigned to format is "Thank you for your donation of {0:####} cans of food to our charitable organization." and arg0 is an integer with the value 10, the return value will be "Thank you for your donation of 10 cans of food to our charitable organization."
Notes to Callers:
In the.NET Framework 4 and the .NET Framework 4.5, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append and AppendFormat methods to append small strings.
The following example demonstrates the AppendFormat method.
using namespace System; using namespace System::Text; using namespace System::Globalization; void Show( StringBuilder^ sbs ) { Console::WriteLine( sbs ); sbs->Length = 0; } int main() { StringBuilder^ sb = gcnew StringBuilder; int var1 = 111; float var2 = 2.22F; String^ var3 = "abcd"; array<Object^>^var4 = {3,4.4,(Char)'X'}; Console::WriteLine(); Console::WriteLine( "StringBuilder.AppendFormat method:" ); sb->AppendFormat( "1) {0}", var1 ); Show( sb ); sb->AppendFormat( "2) {0}, {1}", var1, var2 ); Show( sb ); sb->AppendFormat( "3) {0}, {1}, {2}", var1, var2, var3 ); Show( sb ); sb->AppendFormat( "4) {0}, {1}, {2}", var4 ); Show( sb ); CultureInfo^ ci = gcnew CultureInfo( "es-ES",true ); array<Object^>^temp1 = {var2}; sb->AppendFormat( ci, "5) {0}", temp1 ); Show( sb ); } /* This example produces the following results: StringBuilder.AppendFormat method: 1) 111 2) 111, 2.22 3) 111, 2.22, abcd 4) 3, 4.4, X 5) 2,22 */
Available since 10
.NET Framework
Available since 1.1
AppendFormat Overload
StringBuilder Class
System.Text Namespace
Formatting Types in the .NET Framework
Composite Formatting
Standard Numeric Format Strings
Custom Numeric Format Strings
Standard Date and Time Format Strings
Custom Date and Time Format Strings
Standard TimeSpan Format Strings
Custom TimeSpan Format Strings
Enumeration Format Strings
