C# String Format()

In C#, the string.Format() method is used to format strings by replacing placeholders with corresponding values. It allows you to create complex string representations by specifying formatting options.

The general syntax of string.Format() is as follows:

string formattedString = string.Format(format, arg0, arg1, arg2, ...);

Here’s how it works:

  1. format: This is a composite format string that defines the desired format for the output. It can contain placeholders in the form of {index} or {index:format}. The placeholders are replaced with corresponding arguments specified after the format string.
  2. arg0, arg1, arg2, …: These are the arguments that provide the values to be inserted into the placeholders. The number and types of arguments should match the placeholders in the format string.

Here are a few examples to illustrate the usage of string.Format():

string name = "John";
int age = 25;
string message = string.Format("My name is {0} and I am {1} years old.", name, age);
// Output: "My name is John and I am 25 years old."

double pi = Math.PI;
string formattedPi = string.Format("The value of pi is approximately {0:F2}.", pi);
// Output: "The value of pi is approximately 3.14."

DateTime currentDate = DateTime.Now;
string formattedDate = string.Format("Today's date is {0:yyyy-MM-dd}.", currentDate);
// Output: "Today's date is 2023-05-20."

In the examples above, the placeholders {0}, {1}, and {0:format} are replaced with the corresponding arguments name, age, pi, and currentDate.

You can also use named placeholders or specify formatting options like alignment, padding, number formatting, date and time formatting, and more. The string.Format() method provides a powerful way to create formatted strings in C#.

C# String Format() Method Example:

Certainly! Here’s an example that demonstrates the usage of the string.Format() method in C#:

string name = "Alice";
int age = 30;
double salary = 5000.50;

string formattedString = string.Format("Name: {0}, Age: {1}, Salary: {2:C}", name, age, salary);


Name: Alice, Age: 30, Salary: $5,000.50

Explanation: In this example, we have three variables: name, age, and salary. We use the string.Format() method to create a formatted string representation that includes the name, age, and salary.

The format string "Name: {0}, Age: {1}, Salary: {2:C}" specifies the desired format. Here’s what each part of the format string means:

  • {0}: Represents the first argument (name in this case). It will be replaced with the value of the name variable.
  • {1}: Represents the second argument (age in this case). It will be replaced with the value of the age variable.
  • {2:C}: Represents the third argument (salary in this case) formatted as currency. The :C format specifier formats the value as a currency amount using the appropriate currency symbol and formatting rules based on the current culture.

The resulting formatted string is then stored in the formattedString variable. Finally, we use Console.WriteLine() to display the formatted string in the console.

I hope this example helps you understand how to use the string.Format() method in C# to create formatted strings. Let me know if you have any further questions!