C# String - Format() Method



The C# String Format() method is used to replace one or more format items in a specified string with the string representation of a given object. As an alternative, we might state that this method is used to insert the value of a variable, object, or expression into another string.

It is important to note that this method can be overloaded by passing different types of arguments to it. Let's see basic syntax of it −

Syntax

Following is the syntax of the C# string Format() method −

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

Parameters

This method accepts the following parameters −

  • format: This parameter specifies the required composite format string.
  • args: It represents the object array that contains zero or more objects to format.

Return value

This method returns a string. This is a copy of the format where the string representation of the arguments is used in place of the format item.

Example 1: Basic String Formatting

Following is the basic example of theFormat()method to format the string with placeholders −

using System;
class Program {
   static void Main() {
      int age = 25;
      string name = "Aman";
      // Format the string with placeholders
      string result = string.Format("My name is {0} and I am {1} years old.", name, age);
      Console.WriteLine(result);
   }
}

Output

Following is the output −

My name is Aman and I am 25 years old.

Example 2: Insert a String

Let us look another example of the Format() method. Here, we starts with a format string, followed by one or more objects or expressions that will be converted to strings and inserted at a specified place −

using System;
class Program {
   static void Main() {
      decimal temp = 20.4m;
      string s = String.Format("The temperature is {0}C.", temp);
      Console.WriteLine(s);
   }
}

Output

Following is the output −

The temperature is 20.4C.

Example 3: Using Indexes in Format Strings

In this example, we use the Format() method to reuse the same argument multiple times in the format string −

using System;
class Program {
   static void Main() {
      int x = 5, y = 10;
      // Reusing arguments
      string result = string.Format("First number: {0}, second number: {1}, sum: {2}", x, y, x + y);
      Console.WriteLine(result);
   }
}

Output

Following is the output −

First number: 5, second number: 10, sum: 15

Example 4: Formatting Numbers and Dates

In this example, we use the Format() method to format the numbers and dates using the format specifiers −

using System;
class Program {
   static void Main() {
      double price = 1234.567;
      DateTime currentDate = DateTime.Now;
   
      // Formatting the number to 2 decimal places and formatting the date
      string result = string.Format("The price is {0:C} and today's date is {1:D}.", price, currentDate);
   
      Console.WriteLine("Formated String");
      Console.WriteLine(result); 
   }
}

Output

Following is the output −

Formated String
The price is $1,234.57 and today's date is Wednesday, January 8, 2025.
csharp_strings.htm
Advertisements