C# StringBuilder AppendFormat(IFormatProvider, String, Object[])

Description

StringBuilder AppendFormat(IFormatProvider, 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 using a specified format provider.

Syntax

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


public StringBuilder AppendFormat(
  IFormatProvider provider,/*from   ww  w .ja v  a2  s. c  om*/
  string format,
  params Object[] args
)

Parameters

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

  • provider - An object that supplies culture-specific formatting information.
  • format - A composite format string (see Remarks).
  • args - An array of objects to format.

Returns

StringBuilder.AppendFormat(IFormatProvider, String, Object[]) method returns A reference to this instance after the append operation has completed. After the append operation, this instance contains any data that existed before the operation, suffixed by a copy of format where any format specification is replaced by the string representation of the corresponding object argument.

Example

The following example demonstrates the AppendFormat method.


using System;/*from  ww  w  .j a  v  a2 s .co m*/
using System.Text;
using System.Globalization;

class Sample
{


    public static void Main()
    {
        StringBuilder sb = new StringBuilder();

        float var2 = 2.22F;
        object[] var4 = { 3, 4.4, 'X' };

        CultureInfo ci = new CultureInfo("es-ES", true);
        sb.AppendFormat(ci, "5) {0}", var2);
        Console.WriteLine(sb.ToString());

        Customer customer = new Customer("C", 11111);
         sb = new StringBuilder();
        sb.AppendFormat(new CustomerNumberFormatter(), "{0}: {1}",
                          customer.CustomerNumber, customer.Name);
        Console.WriteLine(sb.ToString());

    }
}
public class Customer
{
    private string custName;
    private int custNumber;

    public Customer(string name, int number)
    {
        this.custName = name;
        this.custNumber = number;
    }

    public string Name
    {
        get { return this.custName; }
    }

    public int CustomerNumber
    {
        get { return this.custNumber; }
    }
}

public class CustomerNumberFormatter : IFormatProvider, ICustomFormatter
{
    public object GetFormat(Type formatType)
    {
        if (formatType == typeof(ICustomFormatter))
            return this;
        return null;
    }

    public string Format(string format, object arg, IFormatProvider provider)
   {
       string custNumber = ((int) arg).ToString("D10");
       return custNumber;

   }
}




















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding