Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 this article 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, Object) Method  
  5. String.Format(IFormatProvider, String, Object, Object, Object) Method
  6. String.Format(IFormatProvider, String, Object[]) Method
  7. String.Format(String, Object, Object) Method
  8. String.Format(String, Object, Object, Object) Method

String.Format(String first, Object second) Method

This method is used to replaces one or more format items in a string with the string representation of a specified object.

Syntax :

public static string Format (string format, object arg0);

Parameter: This method has the following parameters:

format: This parameter is the required composite format string.

arg0: This parameter is the object to format.

Return Value: This method returns the string. It is a copy of format in which any format items are replaced by the string representation of arg0.

Example : 

C#




// C# program to illustrate the 
// String.Format(String first, 
// Object second) Method
  
using System;   
  
public class GFG    
{    
    // Main method 
    public static void Main(string[] args)    
    {   
        DateTime date1 = new DateTime(2019, 11, 11);
          
        // Converts the object to string
        string s1 = string.Format("{0:D}", date1);  
        Console.WriteLine(s1);  
    }    
}


Output:

Monday, 11 November 2019

String.Format(String, params Object[]) Method 

This method is used to replaces the format item in a specified string with the string representation of a corresponding object in a specified array.

Syntax :

public static string Format (string format, params object[] args);

Parameter: This method has the following parameters:

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 args.

Example : 

C#




// C# program to illustrate the 
// String.Format(String, 
// params Object[]) Method
  
using System;   
  
public class GFG    
{    
    // Main method 
    public static void Main(string[] args)    
    {   
        DateTime date1 = new DateTime(2020, 5, 20);
        TimeSpan hiTime = new TimeSpan(14, 17, 32);
        decimal hiTemp = 24.1m; 
        TimeSpan loTime = new TimeSpan(3, 16, 10);
        decimal loTemp = 21.8m; 
          
        // Converts the object to string
        string result1 = String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees"+
                                       " (hi)\n{3,11}: {4} degrees (lo)", date1, 
                                                    hiTime, hiTemp, loTime, loTemp);
        Console.WriteLine(result1); 
    }    
}


Output:

Temperature on 05/20/2020:
   14:17:32: 24.1 degrees (hi)
   03:16:10: 21.8 degrees (lo)

String.Format(IFormatProvider, String, Object) Method 

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

Syntax :

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

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

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,-35:D}", dateToDisplay);
    Console.WriteLine(output);
  }
}


Output:

Wednesday, May 20, 2020 


Last Updated : 28 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads