String.Format() Method in C# with Examples | Set – 2

28 May, 2020
In C#,  Format() is a string method. This method is used to replace one or more format items in the specified string with the string representation of a specified object.In other words, this method is used to insert the value of the variable or an object or expression into another string.
This method can be overloaded by passing different type of arguments to it. There are total 8 methods in the overload list of the Format() method in which 3 are discussed in Set-1 and remaining are discussed in Set-2, and Set-3.
String.Format(String first, Object second) MethodString.Format(String, params Object[]) MethodString.Format(IFormatProvider, String, Object) Method String.Format(IFormatProvider, String, Object[]) MethodString.Format(String, Object, Object) MethodString.Format(String, Object, Object, Object) MethodString.Format(IFormatProvider, String, Object, Object) Method  String.Format(IFormatProvider, String, Object, Object, Object) Method
String.Format(String first, Object second) Method
String.Format(String, params Object[]) Method
String.Format(IFormatProvider, String, Object) Method 
String.Format(IFormatProvider, String, Object[]) Method
String.Format(String, Object, Object) Method
String.Format(String, Object, Object, Object) Method
String.Format(IFormatProvider, String, Object, Object) Method  
String.Format(IFormatProvider, String, Object, Object, Object) Method
This method is used to replaces the format items in a string with the string representations of corresponding objects in a specified array. A parameter supplies culture-specific formatting information.
Syntax :
public static string Format (IFormatProvider provider, string format, params object[] args);

Parameter: This method has the following parameters:
provider: This parameter is the object that supplies culture-specific formatting information.
format: This parameter is the required composite format string.
args: This parameter is the object array that contains zero or more objects to format.
Return Value: This method returns the string. It is a copy of format in which the format items are replaced by the string representation of the corresponding objects in args.
Example : 
C#
// C# program to illustrate the // String.Format(IFormatProvider,// String,Object[]) Method  using System;     public class GFG    {        // Main method     public static void Main(string[] args)        {                     DateTime dateToDisplay =         new DateTime(2020, 5, 20, 18, 32, 0);                  System.Globalization.CultureInfo         culture = new System.Globalization.        CultureInfo("en-US");                  string output = String.Format        (culture, "{0,-11} {1,-35:D}",         culture.Name, dateToDisplay);                   Console.WriteLine(output);    }    }
Output:
en-US       Wednesday, May 20, 2020

This method is used to replaces the format items in a string with the string representation of two specified objects.
Syntax :
public static string Format (string format, object arg0, object arg1);

Parameter: This method has the following parameters:
format: This parameter is the required composite format string.
arg0: This parameter is the first object to format.
arg1: This parameter is the second object to format.
Return Value: This method returns the string. It is a copy of format in which the format items are replaced by the string representation of arg0 and arg1.
Example : 
C#
// C# program to illustrate the // String.Format(String, Object,// Object) Method  using System;     public class GFG    {        // Main method     public static void Main(string[] args)        {                     string formatString =         "Value: {0,0}\n" +         "NOT of Value: {1,0}";                  int value1 = 169;                  string result = String.Format        (formatString, value1, ~value1);                  Console.WriteLine(result);    }    }
Output:
Value: 169
NOT of Value: -170

This method is used to replaces the format items in a string with the string representation of three specified objects.
Syntax :
public static string Format (string format, object arg0, object arg1, object arg2);

Parameter: This method has the following parameters:
format: This parameter is the required composite format string.
arg0: This parameter is the first object to format.
arg1: This parameter is the second object to format.
arg2: This parameter is the third object to format.
Return Value: This method returns the string. It is a copy of format in which the format items are replaced by the string representation of arg0, arg1 and arg2.
Example : 
C#
// C# program to illustrate the // String.Format(String, Object,// Object, Object) Method  using System;     public class GFG    {        // Main method     public static void Main(string[] args)        {                     string formatString =         "Value 1: {0,0}\n" +         "Value 2: {1,0}\n"+        "Sum of Values : {2,0}";                  int value1 = 169;        int value2 = 961;                  string result = String.Format        (formatString, value1, value2,         value1 + value2);                  Console.WriteLine(result);    }    }
Output:
Value 1: 169
Value 2: 961
Sum of Values : 1130

CSharp-method
CSharp-string
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.