System.Text.StringBuilder.AppendFormat Method

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.

Syntax

public StringBuilder AppendFormat (string format, object arg0)

Parameters

format
A composite format string (see Remarks).
arg0
An object to format.

Returns

A reference to this instance with format appended. Each format item in format is replaced by the string representation of arg0.

Exceptions

TypeReason
ArgumentNullException format is a null reference.
FormatException format is invalid.

Remarks

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. 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.

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.Format(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."

Example

C# Example

using System;
using System.Text;

public class StringBuilderTest {
  public static void Main() { 
     
      StringBuilder sb = new StringBuilder("The high ");
      Console.WriteLine( sb.AppendFormat("temperature today was {0, 6}.", 88) ); 
  }
}
      

The output is

Example

The high temperature today was     88.
 

Requirements

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0