Table of contents
1.
Introduction
2.
Format() Syntax
2.1.
C#
3.
Format() Parameters
3.1.
C#
3.2.
C#
4.
Format() Return Value
4.1.
C#
4.2.
C#
5.
Example 1: C# String Format()
5.1.
C#
6.
Example 2: C# Format() With Multiple Format Items
6.1.
C#
7.
Example 3: C# Format() - Control Spacing and Right Alignment
7.1.
C#
8.
Example 4: C# Format() - Control Spacing and Left Alignment
8.1.
C#
9.
Frequently Asked Questions
9.1.
Can I use the Format() method with custom objects?
9.2.
Is it possible to format dates and times using the Format() method?
9.3.
Can I store the formatted string in a variable for later use?
10.
Conclusion
Last Updated: Aug 24, 2024
Easy

C# String Format

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

String formatting in C# is a powerful tool that allows you to create dynamic strings by inserting values into predefined placeholders. It's a way to combine text and variables to make meaningful output. 

C# String Format

In this article, we'll discuss the basics of string formatting in C#, like the Format() method, its syntax, parameters, and return value. In order to understand every concept properly we will look at different types of examples also.

Format() Syntax

The Format() method in C# has the syntax which is mentioned below

string.Format(string format, object arg0, object arg1, ..., object argn)


Here, the "format" parameter is a string that contains text and placeholders for the values you want to insert. The placeholders are specified using curly braces {} and a number that represents the index of the corresponding argument.

The arg0, arg1, ..., argn parameters are the values you want to insert into the placeholders. You can pass any number of arguments, and they will be inserted into the placeholders in the order they appear.

For example:

  • C#

C#

string name = "Rahul";

int age = 25;

string formatted = string.Format("My name is {0} and I am {1} years old.", name, age);

Console.WriteLine(formatted);


Output

My name is Rahul and I am 25 years old.


In this example, {0} is replaced by the value of the "name" variable, and {1} is replaced by the value of the "age" variable.

Format() Parameters

The Format() method takes two types of parameters:

1. Format string: This is a string that contains text and placeholders. Placeholders are specified using curly braces {} and an index number. The index number indicates the position of the argument to be inserted. The format string can also include formatting information for each placeholder, such as the number of decimal places for a floating-point number.
 

2. Arguments: These are the values that will be inserted into the placeholders. You can pass any number of arguments, and they will be inserted in the order they appear in the format string.

For example:

  • C#

C#

string name = "Rahul";

int age = 21;

string city = "Mumbai";

string formatted = string.Format("My name is {0}, I am {1} years old, and I live in {2}.", name, age, city);

Console.WriteLine(formatted);


Output

My name is Rahul, I am 21 years old, and I live in Mumbai.


In this example, the format string contains three placeholders: {0}, {1}, and {2}. The first argument "name" is inserted into {0}, the second argument "age" is inserted into {1}, and the third argument "city" is inserted into {2}.

You can also specify formatting information for each placeholder. For example, you can specify the number of decimal places for a floating-point number:

  • C#

C#

double price = Rs9.99;

string formatted = string.Format("The price is {0:C2}", price);

Console.WriteLine(formatted);


Output

The price is Rs9.99


In this example, the placeholder {0:C2} specifies that the "price" argument should be formatted as a currency with two decimal places.

Format() Return Value

The Format() method returns a string that contains the formatted text. The placeholders in the format string are replaced with the corresponding arguments, and any formatting information is applied.

For example:

  • C#

C#

string name = "Sanjana";

int age = 19;

string formatted = string.Format("My name is {0} and I am {1} years old.", name, age);

Console.WriteLine(formatted);


Output

My name is Sanjana and I am 19 years old.


In this example, the Format() method returns a string that contains the formatted text. The {0} placeholder is replaced with the value of the "name" variable, and the {1} placeholder is replaced with the value of the "age" variable.

It's important to note that the Format() method returns a new string; it does not modify the original format string. If you want to use the formatted string multiple times, you should store the result in a variable.

  • C#

C#

string name = "Harsh";

int score = 95;

string formatted = string.Format("{0} scored {1} points in the exam.", name, score);

Console.WriteLine(formatted);

Console.WriteLine("Great job, " + formatted);


Output

Harsh scored 95 points in the exam.
Great job, Harsh scored 95 points in the exam.


In this example, the formatted string is stored in the "formatted" variable, which is then used multiple times in the Console.WriteLine() statements.

Example 1: C# String Format()

Let's discuss a simple example of using the Format() method to create a formatted string:

  • C#

C#

string name = "Rinki";

int age = 20;

string city = "Delhi";

string formatted = string.Format("My name is {0}, I am {1} years old, and I live in {2}.", name, age, city);

Console.WriteLine(formatted);


Output

My name is Rinki, I am 20 years old, and I live in Delhi.


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

The format string contains three placeholders: {0}, {1}, and {2}. The first argument "name" is inserted into {0}, the second argument "age" is inserted into {1}, and the third argument "city" is inserted into {2}.

The Format() method returns a new string that contains the formatted text, which we store in the "formatted" variable. Finally, we use Console.WriteLine() to print the formatted string to the console.

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

In addition to inserting values into placeholders, you can also use the Format() method to format the values themselves. You can specify the format for each placeholder using format items.

For example:

  • C#

C#

string name = "Ravi";

int age = 22;

double height = 5.11;

string formatted = string.Format("My name is {0}, I am {1:D3} years old, and I am {2:F2} feet tall.", name, age, height);

Console.WriteLine(formatted);


Output

My name is Ravi, I am 022 years old, and I am 5.11 feet tall.


In this example, we have three variables: "name", "age", and "height". We use the Format() method to create a formatted string that includes these variables, but we also specify the format for each placeholder.

The format string contains three placeholders: {0}, {1:D3}, and {2:F2}. The first placeholder {0} doesn't have any format specified, so the value of "name" is inserted as is. The second placeholder {1:D3} specifies that the "age" argument should be formatted as a decimal number with a minimum of three digits. The third placeholder {2:F2} specifies that the "height" argument should be formatted as a floating-point number with two decimal places.

The Format() method returns a new string that contains the formatted text, which we store in the "formatted" variable. Finally, we use Console.WriteLine() to print the formatted string to the console.

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

In addition to formatting the values themselves, you can also use the Format() method to control the spacing and alignment of the values within the placeholders.

For example:

  • C#

C#

string name = "Mehak";

int age = 23;

string city = "Pune";

string formatted = string.Format("|{0,10}|{1,5}|{2,10}|", name, age, city);

Console.WriteLine(formatted);


Output

|     Mehak|   23|      Pune|


In this example, we have three variables: "name", "age", and "city". We use the Format() method to create a formatted string that includes these variables, but we also specify the spacing and alignment for each placeholder.

The format string contains three placeholders: {0,10}, {1,5}, and {2,10}. The first placeholder {0,10} specifies that the "name" argument should be right-aligned within a field of width 10. The second placeholder {1,5} specifies that the "age" argument should be right-aligned within a field of width 5. The third placeholder {2,10} specifies that the "city" argument should be right-aligned within a field of width 10.

We also include vertical bars (|) in the format string to create a table-like structure. The Format() method returns a new string that contains the formatted text, which we store in the "formatted" variable. Finally, we use Console.WriteLine() to print the formatted string to the console.

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

In the previous example, we saw how to use the Format() method to control spacing and right alignment of values within placeholders. We can also use it to control spacing and left alignment.

For example:

  • C#

C#

string name = "Sinki";

int age = 24;

string city = "Kolkata";

string formatted = string.Format("|{0,-10}|{1,-5}|{2,-10}|", name, age, city);

Console.WriteLine(formatted);


Output

|Sinki     |24   |Kolkata   |


In this example, we have three variables: "name", "age", and "city". We use the Format() method to create a formatted string that includes these variables, but we also specify the spacing and alignment for each placeholder.

The format string contains three placeholders: {0,-10}, {1,-5}, and {2,-10}. The first placeholder {0,-10} specifies that the "name" argument should be left-aligned within a field of width 10. The second placeholder {1,-5} specifies that the "age" argument should be left-aligned within a field of width 5. The third placeholder {2,-10} specifies that the "city" argument should be left-aligned within a field of width 10.

The negative number after the comma in each placeholder indicates left alignment. We also include vertical bars (|) in the format string to create a table-like structure. The Format() method returns a new string that contains the formatted text, which we store in the "formatted" variable. Finally, we use Console.WriteLine() to print the formatted string to the console.

Frequently Asked Questions

Can I use the Format() method with custom objects?

Yes, you can use the Format() method with custom objects by overriding the ToString() method in your class.

Is it possible to format dates and times using the Format() method?

Yes, you can format dates and times using the Format() method by specifying the appropriate format specifier.

Can I store the formatted string in a variable for later use?

Yes, you can store the result of the Format() method in a variable and use it later in your program.

Conclusion

In this article, we learned about the string Format() method in C#. We discussed its syntax, parameters, and return value. We also looked at several examples that showed how to use the Format() method to create formatted strings, control spacing, and align values within placeholders. The Format() method is a powerful tool that allows you to create dynamic and neatly formatted output in your C# programs.

You can also check out our other blogs on Code360.

Live masterclass