This documentation is archived and is not being maintained.

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 corresponding argument in a parameter array.

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)

Public Function AppendFormat ( _
	format As String, _
	ParamArray args As Object() _
) As StringBuilder


Type: System.String
A composite format string (see Remarks).
Type: System.Object()
An array of objects 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 the corresponding object argument.


format or args is Nothing.


format is invalid.


The index of a format item is less than 0 (zero), or greater than or equal to the length of the args array.


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, 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:


Elements in square brackets are optional. The following table describes each element.




The zero-based position in the parameter list of the object to be formatted. If the object specified by index is Nothing, the format item is replaced by String.Empty. If there is no parameter in the index position, a FormatException is thrown.


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.


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.

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.

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, when you instantiate the StringBuilder object by calling the StringBuilder 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.

Imports System
Imports System.Text
Imports System.Globalization

Class Sample
   Private Shared sb As New StringBuilder()

   Public Shared Sub Main()
      Dim var1 As Integer = 111
      Dim var2 As Single = 2.22F
      Dim var3 As String = "abcd"
      Dim var4 As Object() =  {3, 4.4, "X"c}

      Console.WriteLine("StringBuilder.AppendFormat method:")
      sb.AppendFormat("1) {0}", var1)
      sb.AppendFormat("2) {0}, {1}", var1, var2)
      sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3)
      sb.AppendFormat("4) {0}, {1}, {2}", var4)
      Dim ci As New CultureInfo("es-ES", True)
      sb.AppendFormat(ci, "5) {0}", var2)
   End Sub 'Main

   Public Shared Sub Show(sbs As StringBuilder)
      sb.Length = 0
   End Sub 'Show
End Class 'Sample
'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

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.