Site icon

Usage of String.Format() Method in C#

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.

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}

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.

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}

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.

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

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

Exit mobile version