In C#, String.Format() method is generally used to insert variable, expression or value of an object into another string variable. It is a static method of String class and takes a input string that identifies replaceable parameters by using numbers in curly braces.

For example : String.Format("My name is : {0}", name);

The simplest form of String.Format method to format objects and variables is as follows.

  • string format (Input parameter) : Its the input string consists of fixed text intermixed with indexed placeholders, i.e curly braces, that correspond to the objects in the list.
  • object? arg (Input parameter) : The object to be used for formatting.

Example of String.Format() in C# :

In below example, we insert the string name = "James"; to another string "My Name is : {0}" in {0} index . The result string becomes as “My Name is James”.

Example of inserting multiple item in to String.Format() in C# :

In below example, we are inserting a string value and an integer value into another string using String.Format method.

In above, string string strFormat = String.Format("{0} is {1} years old", name, age);  tells the Format method to replace {0} with first parameter valueJames , and replace {1} with the second parameter value “25”. The resultant string becomes as James is 25 years old .

Example of inserting an object in to String.Format() in C# :

In below example, we have created a Student class and in Main method we are inserting Student property values in to another string using String.Format method.

So in the {0} index it inserted stu.FirstName as “James“, in the {1} index it inserted stu.LastName as “Cameron” and in the {2} index , it inserted stu.EnrollmentDate.ToString("MM/dd/yyyy") value as “04-12-2019“.

When you run above example, then it prints James Cameron Enrollment Date is on : 04-12-2019as the final string.

Formatting of a string using String.Format() in C# :

We can also format the object using control formatting syntax inside Format method. It uses below syntax for formatting an object.

{index number : formatting expression}

  • index number :  The zero-based index of the argument, it would be replaced with the string representation at this position, if the argument is null then empty string will be replaced.
  • formatting expression : It is the formatting expression of the result string out put.

For example, in below code we have applied formatting information as {2:D} for the index number 2 parameter stu.EnrollmentDate

When we run above sample code, then it formats the stu.EnrollmentDate value to “12 April 2019“. The final output thus becomes James Cameron Enrollment Date is on : 12 April 2019 .

When we run above code, then it generates below output.

String.Format Out_Put 1.0

To check more formatting type and option, please visit Format types in .NET

Alignment using String.Format() in C# :

Along with formatting the object value, we can also do alignment and add spacing to it. You can define the width of the string that is inserted into the result string.

It uses syntax such as {0,10} , that means it will inserts 10-character string in to the result string value. Following is the syntax and different conditions of adding spaces to object using String.Format method.

{index number , alignment value}

  • alignment value : A signed integer value, that defines the width of string that is going to be inserted to the result string. To left-align strings in a field, you have to provide negative value, such as {0,-10} and to right-aligned, you have to provide a positive value, such as {0,10}

We can also include formatting string along with the alignment value to the expression as follows.

{index number , alignment value : formatting expression} 

Lets look at below example to understand how this alignment and formatting expression works.

When we run above program then it generates below output.

String.Format Out_Put 1.1

If you look at the output, then you can find that we have implemented.

  • Left alignment by using {0,-20} {1,-15} for both first and second argument.
  • Right alignment by using {0,20} {1,15} for both arguments.
  • The formatting expression {0,-20:g} {1,-15:N1} and {0,20:g} {1,15:N1} to format the result string.

Thanks for visiting, Please provide your feedback and do check my other articles.