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

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 6 are discussed in Set-1 and Set-2, and remaining are discussed in this article. 

  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

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

This method is used to replaces the format items in a string with the string representation of two specified objects. A parameter supplies culture-specific formatting information.

Syntax :

public static string Format (IFormatProvider provider, string format, object arg0, object arg1);

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.

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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// String.Format(IFormatProvider,
// 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;
          
         System.Globalization.CultureInfo culture = 
        new System.Globalization.CultureInfo("es-ES");
          
        string result = String.Format
        (culture, formatString, value1, ~value1);
          
        Console.WriteLine(result);
    }    
}

chevron_right


 

Output:

Value: 169
NOT of Value: -170

String.Format(IFormatProvider, 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. An parameter supplies culture-specific formatting information.



Syntax :

public static string Format (IFormatProvider provider, string format, object arg0, object arg1, object arg2);

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.

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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// String.Format(IFormatProvider,
// 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;
          
         System.Globalization.CultureInfo culture = 
        new System.Globalization.CultureInfo("es-ES");
          
        string result = String.Format
        (culture, formatString, value1, value2, 
        value1 + value2);
          
        Console.WriteLine(result);
    }    
}

chevron_right


 

Output:

Value 1: 169
Value 2: 961
Sum of Values : 1130



My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.