find and solve || findandsolve.com
Please wait.....

C# String Format() Method Example

The C# Format() method is used to replace one or more format items in the specified string with the string representation of a specified object.

The string Format method is used in c# to insert the variable's value or an object or expression into another string.  You can replace the format items in the specified string with the string representation of specified objects using the string Format method.

In the given below example representation of using a string Format() method to insert a variable or expression values into another string in the c# programming language.

string s="Address:{0}{1}"//string
Format(s,"Rahul","Sharma")//Method
Name= Rahul Sharma//Out put

If you observe the above example, we inserted format items inside of string and replaced those formatted items with the specified string values using the Format method.

C# String Format Method Syntax

In the given below syntaxes of defining a string Format method to replace inserted format items within the string using specified string objects in the c# programming language.

public string Format(string, object)
public string Format(string, object, object)
public string Format(IFormatProvider, string, object)

In the above syntaxes, the first syntax replaces one or more format items in a string with the string representation of a specified object.

And then the second syntax replaces the format items in a string with the string representation of two specified objects. And in the third syntax replaces the format items in a specified string with the string representation of the corresponding object.

C# String Format Method Example

In the given below an example of using a string Format() method to insert an object or variable or expression value into another string in the c#. 

using System;
namespace FindAndSolve
{     class Program     {        static void Main(string[] args)        {            string s = "Name:{0} {1}, Address:{2}, Hight:{3}";            string msg = string.Format(s, "Rahul", "Sharma", "Dallas", 6);            Console.WriteLine("Format Result: {0}", msg);            Console.WriteLine("\nPress Enter Key to Exit..");            Console.ReadLine();        }     } } 
Output
Format Result: Name:Rahul Sharma, Address:Dallas, Hight:6

If you observe the given above result, the string Format() method has replaced format items {0} with “Rahul”, {1} with “Sharma”, {2} with “Dallas” and {3} with “6” in the given string based on our requirements.

C# Number Formats

In the given example lists the different types of number formats in c# to format the given numerical values. For example, you can  passing the decimal value is 66568.72378981.


CharacterDescriptionUsageExample
cCurrency{0:c} $66,568.72
eScientific{0:e}6.656872e+004
fFixed Point {0:f}66568.72
gGeneral{0:g}66568.72378981
nThousand Separator{0:n}66,568.72

C# Date Time Formats

The DateTime data type use in C# date and time. It’s provides properties and methods to format dates in different datetime formats in C#. For more details Date Time Formats