Formatted string

We can append formatted string to StringBuilder with AppendFormat method.

AppendFormat works the same way with string.Format.


using System;
using System.Text;
class Sample
{
    public static void Main()
    {
        string composite = "Name={0,-20} Account={1,15:C}";

        StringBuilder sb = new StringBuilder();

        sb.AppendFormat(composite, "M", 999999);
        Console.WriteLine(sb);
    }
}

The output:


Name=M                    Account=    $999,999.00
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.