Introduction:

In this article we will explain how to use string Format method or what is string Format method in c# with example.

Description:

In c# programming language the string Format method is used to insert the value of variable or an object or expression into another string. By using string Format method, we can replace the format items in the specified string with the string representation of specified objects.


C# String Format Method Example

Following is the example of using string Format() method to insert an object or variable or expression value into another string in c# programming language.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace SQLTutorialsPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Name:{0} {1}, Address:{2}, Age:{3}";
            string msg = string.Format(s, "Manoj", "Kumar", "Mumbai", 22);
            Console.WriteLine("Format Result: {0}", msg);
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

 

As you see above example, we used a string Format() method to replace a format items in given string.

 

When we execute above c# program, we will get the result like as shown below.

 

 How to use String Format Method in c#

 

As you see above result, the string Format() method has replaced a format items {0} with “Manoj”, {1} with “Kumar”, {2} with “Mumbai” and {3} with “22” in given string based on our requirements.

 

In this article we have learned, how we can use string Format() method to insert format items into given string and replace those items with their respective values in c# programming language.   

 

I hope it will help to you after reading.

 

 

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like