C# StringBuilder AppendFormat(String, Object)

Description

StringBuilder AppendFormat(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.

Syntax

StringBuilder.AppendFormat(String, Object) has the following syntax.


public StringBuilder AppendFormat(
  string format,
  Object arg0
)

Parameters

StringBuilder.AppendFormat(String, Object) has the following parameters.

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

Returns

StringBuilder.AppendFormat(String, Object) method returns A reference to this instance with format appended. Each format item in format is replaced by the string representation of arg0.

Example

The following example demonstrates the AppendFormat method.


//from   w w  w .  j  a  va2 s .  c  o  m
using System;
using System.Text;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
        StringBuilder sb = new StringBuilder();
        int    var1   = 111;
        float  var2   = 2.22F;
        string var3   = "abcd";
        object[] var4 = {3, 4.4, 'X'};
    
        Console.WriteLine();
        Console.WriteLine("StringBuilder.AppendFormat method:");
        sb.AppendFormat("1) {0}", var1);
        Console.WriteLine(sb.ToString());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding