How to use the string.format method in C#?
The string.Format method in C# is used to apply a specified format to the parameters in a string. Here is the basic syntax for using the string.Format method:
string result = string.Format(format, arg0, arg1, ...);
- The format is a string used to specify the output format. Placeholders such as {0}, {1}, etc. can be used to insert parameters into the string.
- arg0, arg1, and so on are the parameters to be inserted into the string.
Here is an example showing how to use the string.Format method:
string name = "John";
int age = 25;
string message = string.Format("My name is {0} and I am {1} years old.", name, age);
Console.WriteLine(message);
The output result is:
My name is John and I am 25 years old.
In the example above, we used {0} and {1} as placeholders and passed the name and age parameters as arguments to string.Format.