String.Format(String, Object, Object, Object) Method

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


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

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.

  1. String.Format(String first, Object second) Method
  2. String.Format(String, params Object[]) Method
  3. String.Format(IFormatProvider, String, Object) Method 
  4. String.Format(IFormatProvider, String, Object[]) Method
  5. String.Format(String, Object, Object) Method
  6. String.Format(String, Object, Object, Object) Method
  7. String.Format(IFormatProvider, String, Object, Object) Method  
  8. String.Format(IFormatProvider, String, Object, Object, Object) Method

Similar Reads

String.Format(IFormatProvider, String, 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....

String.Format(String, Object, Object) Method

...

String.Format(String, Object, Object, Object) Method

This method is used to replaces the format items in a string with the string representation of two specified objects....