C# String Format()

The Format() method returns a formatted string based on the argument passed.

Example

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string name = "Programiz";
// format string string strFormat = String.Format("Hello {0}", name);
Console.WriteLine(strFormat); } } } // Output: Hello Programiz

Format() Syntax

The syntax of the string Format() method is:

String.Format(String format, Object...args);

Here, Format() is a static method. Hence, we have used the class name String to call it.


Format() Parameters

The String.Format() method takes two parameters:

  • format - a format string
  • args - the object to format

Format() Return Value

The Format() method returns a formatted string.


Example 1: C# String Format()

// C# Program to insert the value of a single variable in a string

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      int number = 2;
// format string string strFormat = String.Format("There are {0} apples.", number);
Console.WriteLine(strFormat); } } }

Output

There are 2 apples.

In the above code, notice the line

string strFormat = String.Format("There are {0} apples.", number);

Here,

  • "There are {0} apples." is a format string
  • {0} is the format item
  • the variable number is inserted in the place of {0}

Example 2: C# Format() With Multiple Format Items

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string name = "Ed Sheeran";
      string food = "apple";

// format string string strFormat = String.Format("{0} eats {1}", name, food);
Console.WriteLine(strFormat); Console.ReadLine(); } } }

Output

Ed Sheeran eats apple

In the above example, notice the line,

string strFormat = String.Format("{0} eats {1}", name, food);

Here,

  • we have two format items {0} and {1}
  • {0} is replaced by the first object passed in the method i.e. name
  • {1} is replaced by the second object passed in the method i.e. food

Example 3: C# Format() - Control Spacing and Right Alignment

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

// format string string strFormat = String.Format("{0, 20}", "Programiz");
Console.WriteLine(strFormat); Console.ReadLine(); } } }

Output

       	Programiz

In the above example, notice the line.

string strFormat = String.Format("{0, 20}", "Programiz");

Here,

  • 0 represents the first object in the method
  • 20 specifies the width of the string
  • since 20 is a positive number, "Programiz" is right-aligned

Example 4: C# Format() - Control Spacing and Left Alignment

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

// format string string strFormat = String.Format("{0, -20} {1, -14}", "Programiz", "C# Programming");
Console.WriteLine(strFormat); Console.ReadLine(); } } }

Output

Programiz        	C# Programming

In the above example, we have used the format string "{0, -20} {1, -14}". Here,

  • 0 and 1 represent the first and second objects in the method ("Programiz" and "C# Programming")
  • -20 and -14 represent the width of "Programiz" and "C# Programming" respectively.

Since -20 and -14 are negative numbers, "Programiz" and "C# Programming" are left-aligned.

Frequently Asked Questions

How to format dates using String.Format()?

We can format dates using String.Format() in the following way:

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      DateTime date = new DateTime(2015, 12, 31); 

// format string string strDate = String.Format("{0:D}", date);
Console.WriteLine(strDate); Console.ReadLine(); } } }

Output

Thursday, December 31, 2015

In the above example, notice the line,

string strDate = String.Format("{0:D}", date);

The {0:D} format item specifies the first object passed in the method will be formatted in the long date format.

What are the common date format specifiers in String.Format()?

Some of the common date format specifiers are as follows:

Format Specifier Description Example
d Short date 9/17/2021
D Long date Friday, September 17, 2021
t Short time 4:11 PM
T Long time 4:11:34 PM
M Month September 17
Y Year September 2021
How to format numbers using String.Format()?

We can format numbers using String.Format() in the following way:

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

// format string string strDecimal = String.Format("Decimal: {0:D}", 200); string strHexaDecimal = String.Format("Hexadecimal: {0:X}", 200);
Console.WriteLine(strDecimal); Console.WriteLine(strHexaDecimal); Console.ReadLine(); } } }

Output

Decimal: 200
Hexadecimal: C8

In the above example, notice the line,

string strDecimal = String.Format("Decimal: {0:D}", 200);

string strHexaDecimal = String.Format("Hexadecimal: {0:X}", 200);

Here,

  • {0:D} - specifies the first object passed in the method will be formatted in decimal
  • {0:X} - specifies the first object passed in the method will be formatted in hexadecimal
What are the common number format specifiers in String.Format()?

Some of the common number format specifiers are as follows:

Format Specifier Description Example
N Number 200.00
E Scientific 2.000000E+002
C Currency $200.00
P Percentage 20,000.00%
Did you find this article helpful?