C# string format methods

In this source code example, we will see three different methods to format strings in C#.

C# string format methods

In the following example, we use string.Format(), Console.WriteLine(), and StringBuilder.AppendFormat() to format strings.
using System;
using System.Text;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
     	    var msg = string.Format("There are {0} owls", 5);
            Console.WriteLine(msg);

            Console.WriteLine("There are {0} eagles", 3);

            var builder = new StringBuilder();
            builder.AppendFormat("There are {0} hawks", 4);
            Console.WriteLine(builder);
     }
  }
}

Output:

There are 5 owls

There are 3 eagles

There are 4 hawks

Comments