C# String Format Method

In c#, 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.

 

Following is the pictorial representation of using string Format() method to insert a variable or an expression values into another string in c# programming language.

 

C# String Format Method Representation Diagram

 

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

C# String Format Method Syntax

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

 

public string Format(string, object)

public string Format(string, object, object)

public string Format(IFormatProvider, string, object)

If you observe above syntaxes, first syntax is used to replaces one or more format items in a string with the string representation of a specified object.

 

The second syntax is used to replaces the format items in a string with the string representation of two specified objects and the third syntax is used to replaces the format items in a specified string with the string representation of corresponding object.

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;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            string s = "Name:{0} {1}, Location:{2}, Age:{3}";

            string msg = string.Format(s, "Suresh", "Dasari", "Hyderabad", 32);

            Console.WriteLine("Format Result: {0}", msg);

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe 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.

 

C# String Format Method Example Result

 

If you observe above result, the string Format() method has replaced a format items {0} with “Suresh”, {1} with “Dasari”, {2} with “Hyderabad” and {3} with “32” in given string based on our requirements.

C# Format String Items

In above example, we used a string Format() method to replace format items within the string but by using Format() method we can control appearance of format items.

 

Following is the example of controlling the appearance of format items using string Format() method in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            decimal num = 75.73789621m;

            DateTime datetime = DateTime.Now;

            Console.WriteLine("Format Decimal: {0:n2}", num);

            Console.WriteLine("DateTime: {0}", datetime);

            Console.WriteLine("Only Date: {0:D}", datetime);

            Console.WriteLine("Only Time: {0:T}", datetime);

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above code, we are formatting given decimal number to only decimal value using {0:n2} and formatting datetime value to display only date {0:d} or time {0:t} based on our requirements.

 

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

 

C# Format String Items using Format Method Example Result

 

If you observe above result, we are formatting the given item values using Format() method based on our requirements.

 

In c#, we have a different formats to format the object values using Format() method. Following tables lists the different type of formats available in c# programming language.

C# Date Time Formats

Following table lists the different type of date time formats available in c# to format the given datetime values. Generally the Date/Time formats are depend on the user locale so the output may be different.

 

CharacterDescriptionUsageExample
d Short Date {0:d} 29-05-2018
D Long Date {0:D} 29 May 2018
t Short Time {0:t} 05:29:20
T Long Time {0:T} 05:29:20
f or F Long Date Time {0:f} 29 May 2018 05:30:08
g or G Short Date Time {0:g} 29-05-2018 05:31:42
M Short Date {0:M} May 29
r RFC1123 Date Time String {0:r} Tue, 29 May 2018 05:33:02 GMT
s Sortable Date/Time {0:s} 2018-05-29T05:34:10
u Universal Sortable Date {0:u} 2018-05-29 05:35:47Z
U Universal full date {0:U} 29 May 2018 00:08:07
Y Year month pattern {0:Y} May, 2018

C# Number Formats

Following table lists the different type of number formats available in c# to format the given number values. For example, we are passing decimal value as 75674.73789621.

 

CharacterDescriptionUsageExample
c Currency {0:c} $ 75,674.74
e Scientific {0:e} 7.567474e+004
f Fixed Point {0:f} 75674.74
g General {0:g} 75674.73789621
n Thousand Separator {0:n} 75,674.74

C# Custom Formats

In c#, we can also use custom formats to format the string. Following table lists the different type of custom formats which we can use to format the strings. For example, we are passing decimal value as 75674.73789621.

 

CharacterDescriptionUsageExample
0 Zero Placeholder {0:00.00} 75674.74
# Digit Placeholder {0:(#).##} (75674).74
. Decimal Point {0:0.000} 75674.738
, Thousand Separator {0:0,0} 75,675
% Percent {0:0%} 7567474%

This is 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.