StringBuilder: AppendFormat

AppendFormat

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.



 Public Function AppendFormat(
	  ByRef Format As String,
	  ParamArray Args ( ) As Variant ) As StringBuilder

Parameters

Format
[ByRef] String. The composite format string containing formatting information.
Args
[ByRef] Variant. The arguments to be formatted into the string.

Return Values

StringBuilder -  This instance of StringBuilder.

Remarks

This method uses the composite formatting feature of VBCorLib 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 values in the parameter list of this method. The formatting process replaces each format item with the string representation of the corresponding value.

The syntax of a format item is as follows:

{index[,length][:formatString]}

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

ElementDescription
indexThe 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 an empty string. If there is no parameter in the index position, a FormatException is thrown.
,lengthThe minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.
:formatStringA standard or custom format string that is supported by the parameter.

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).ToString(formatString, provider) defines the formatting. Otherwise, Object.ToString(Args(index)) defines the formatting.

Examples

This example uses the AppendFormat method to insert values into the string with formatting information. The output will show how the formatting information was used to transform the arguments into a formatted output.

Private Sub Main()
    Dim sb As New StringBuilder
    
    ' Indicates the index of the supplied
    ' arguments to be inserted into the string.
    sb.AppendFormat "My name {0}.", "Kelly"
    sb.AppendLine
    
    ' Insert an integer value that is 5 digits
    ' in length, prepending leading zeros if necessary.
    sb.AppendFormat "A number with leading zeros: {0:d5}.", 23
    sb.AppendLine
    
    ' Inserts the value into a column of 10 characters
    ' with alignment to the right of the column.
    sb.AppendFormat "Right aligned 10 character column: '{0,10}'.", "right"
    sb.AppendLine
    
    ' Inserts the value into a column of 10 characters
    ' with alignment fo the left of the column.
    sb.AppendFormat "Left aligned 10 character column: '{0,-10}'.", "left"
    sb.AppendLine
    
    ' To prevent the insertion of an argument and allow
    ' for curly braces to be inserted into the string, two
    ' braces must be placed together to cause an escape from
    ' the formatting sequence.
    sb.AppendFormat "Use two braces to put a single brace in the output without formatting. {{0}}", "Not Used"
    
    ' Display the contents of the StringBuilder
    Debug.Print sb.ToString
End Sub

' This code produces the following output.
'
' My name Kelly.
' A number with leading zeros: 00023.
' Right aligned 10 character column: '     right'.
' Left aligned 10 character column: 'left      '.
' Use two braces to put a single brace in the output without formatting. {0}

See Also

Project CorLib Overview

Class StringBuilder Overview

NumberFormatInfo

DateTimeFormatInfo