Demonstrating StringBuilder AppendFormat

image_pdfimage_print

   

    using System;
    using System.Text;

   class StringBuilderAppendFormat {
      static void Main(string[] args)
      {
         StringBuilder buffer = new StringBuilder();
         string string1, string2;

         string1 = "This {0} costs: {1:C}.
";

         object[] objectArray = new object[ 2 ];

         objectArray[ 0 ] = "Software";
         objectArray[ 1 ] = 1234.56;
         buffer.AppendFormat( string1, objectArray );

         string2 = "Number:{0:d3}.
" +
            "Number right aligned with spaces:{0, 4}.
" +
            "Number left aligned with spaces:{0, -4}.";

         buffer.AppendFormat( string2, 5 );
         Console.WriteLine(buffer.ToString());
      }
   }