In C#, string formatting is like combining different things such as strings, values, and expressions to create a cool formatted string. It’s like setting up a template with placeholders that will be replaced with actual values when your code runs.
Why is this so important? Well, it helps make your output more readable and easier to understand. You can mix text and values in a way that makes sense to whoever’s reading it. Plus, it’s a handy way to tackle tricky formatting situations without too much hassle.
C# String Format
Now, when I started out learning to code, then formatting String was something that confused me a lot. Therefore, this post offers possible methods to format a string in C#.
Method 1: Using the String Interpolation
Hey, did you know that you can format your strings in C# using variables and placeholders? It’s called String Interpolation and it’s pretty cool! All you have to do is use these placeholders and then replace them with their respective values. This process is known as string interpolation, and it’s a great way to make your code look neat and tidy.
Syntax:
string str = $"Welcome to {var1}, Naveed-Ul-Haq's Blog {var2}";
You can clearly see the working through the syntax, like, you need to put “$” before the string starts. After that, you need to start the string and put the variable names in the braces as shown.
Want more understanding? Let’s practice it through the example:
Example:
This example presents the application of the format specifiers in the string where two substrings are being used in the string:
string niche = "C#, .NET, Azure, etc";
string topic = "Format String in C#";
string res = string res = $"You are at Naveed's Blog, which offers tutorials on {niche}, \nand today's post is about {topic}";
Console.WriteLine(res);
Here, two variables are initialized “niche” and “topic” which are used furtherly in the string “res”. Let’s see the output:
Output:

Here, the placeholders have been added and the string is formatted for better readability.
Method 2: Using Format() Method
Check it out, there’s a method in C# that lets you combine values of variables with a format string to make a fancy formatted string! The placeholders for the variables are marked with braces ({}) and you can even specify which variable goes where by using its index or name.
Syntax:
string res = String.Format("Welcome {0}, to Naveed’s Blog {1}", var1, var2);
Here, the syntax seems to be the same as above. The variables are called inside the string with the “index” number in the braces “{}” and the var1, and var2 refer to the variable names.
Example 1:
string var1= "Naveed Ul Haq";
String blog = "naveedulhaq.com"
string res = String.Format("Hello, I am {0}, and I blogs at {1}", var1, var2);
Console.WriteLine(res);
Now, there are two variables “var1” and “var2”, these are called in the string using their index numbers, i.e, var1 and var2 refer to the {0} and {1} “general format specifier to call a variable”.
Output:

Look at the output, the general format specifiers have been replaced with the value of the variable.
Example 2: Format the Decimal
The decimal numbers sometimes have a very long number after the float time. The decimal point can be restricted to a specific decimal point:
Code:
double num = 0.123;
string str = string.Format("The Number is: {0:F2}", num);
Console.WriteLine(str);
This will get the current date and time and show it on the provided syntax:
Output:

The number has been restricted to 2 decimal places.
Note: The core functionality of the string format depends on the format specifiers. Navigate to Microsoft’s official documentation to get the supported format specifiers.
Method 3: Using String Concatenation
String concatenation also helps in getting the formatted string. Just you need to add the “+” sign to add the variable with the string:
Code:
int a=10;
string b = " Tens of tutorials on";
string str = String.Format("Welcome to N-UL-HAQ" + b + " C#/.NET/Azure" + a);
Console.WriteLine(str);
The above code has two variables one is a string and the other is an integer. These are concatenated with the “str” variable.
Output:

The output shows that both variables are concatenated and the string is formatted.
Wrap Up
So, Format String is basically a way to get a nicely formatted version of a string for easy reading and consistent output. In C#, which is pretty advanced, there are a bunch of methods you can use to format strings, like the Format() method and string interpolation. You use these methods together with format specifiers to get the result you want.
This post has listed all these methods to format a string in C#.
If you want to learn the C# switch statement visit my blog on C# switch. Get in touch with this Blog to stay updated with the tutorials on C#.