How to use C# string Format

In C#, the string Format method is a powerful tool for formatting strings by replacing placeholder elements with corresponding values. This method allows developers to create dynamic strings that incorporate variables, values, and other data types in a structured and customizable manner.

The Format method follows a specific syntax using curly braces {} as placeholders within a format string. The placeholders are then replaced with the desired values provided as arguments to the Format method. These arguments can be variables, constants, expressions, or other strings.

Here's an example to illustrate how the Format method works:

string name = "Smith"; int age = 30; double height = 1.75; string formattedString = string.Format("My name is {0}, I am {1} years old, and my height is {2} meters.", name, age, height); Console.WriteLine(formattedString); //Output:My name is Smith, I am 30 years old, and my height is 1.75 meters.

In the above example, we have a format string with three placeholders {0}, {1}, and {2}. These placeholders correspond to the values of name, age, and height, respectively. By passing these variables as arguments to the Format method, the placeholders get replaced with their corresponding values, resulting in a formatted string.

The Format method also supports various formatting options to control the appearance of the values within the placeholders. For example, you can specify the number of decimal places, add currency symbols, specify date and time formats, and more.

Here's another example demonstrating formatting options:

decimal price = 9.99m; int quantity = 5; string formattedString = string.Format("The total cost is {0:C2} for {1} items.", price * quantity, quantity); Console.WriteLine(formattedString); //Output: The total cost is $49.95 for 5 items.

In this example, we use the formatting option ":C2" within the placeholder {0}. This option formats the value as a currency with two decimal places, resulting in a formatted string with the dollar symbol and proper formatting.

The Format method offers a wide range of formatting options and supports a variety of data types. It provides a flexible and efficient way to construct complex strings with dynamic content, making it a valuable tool for generating user-friendly messages, logging, report generation, and other string manipulation scenarios in C# applications.

Formatting Numeric Values

int number = 42; double amount = 1234.56789; string formattedNumber = string.Format("The number is: {0:D5}", number); string formattedAmount = string.Format("The amount is: {0:C2}", amount); Console.WriteLine(formattedNumber); // Output: The number is: 00042 Console.WriteLine(formattedAmount); // Output: The amount is: $1,234.57

In the first example, we use the formatting option ":D5" within the placeholder {0}. This option formats the integer value with leading zeros to a width of five characters. In the second example, the formatting option ":C2" formats the double value as currency with two decimal places.

Formatting Date and Time Values

DateTime now = DateTime.Now; string formattedDate = string.Format("Today is: {0:dd/MM/yyyy}", now); string formattedTime = string.Format("The current time is: {0:HH:mm:ss}", now); Console.WriteLine(formattedDate); // Output: Today is: 16/07/2023 Console.WriteLine(formattedTime); // Output: The current time is: 15:30:45

In this example, we utilize different formatting options for the DateTime value. The ":dd/MM/yyyy" option formats the date as day/month/year, and the ":HH:mm:ss" option formats the time as hours:minutes:seconds.

Custom Formatting Options

decimal value = 12345.6789m; string formattedValue1 = string.Format("The value is: {0:#,##0.00}", value); string formattedValue2 = string.Format("The value is: {0:(0.00)}", value); Console.WriteLine(formattedValue1); // Output: The value is: 12,345.68 Console.WriteLine(formattedValue2); // Output: The value is: (12,345.68)

In this example, we define custom formatting options. The "#,##0.00" option formats the decimal value with a comma as a thousand separator and two decimal places. The "(0.00)" option formats the value in parentheses, indicating a negative value.

Formatting Strings

string firstName = "Smith"; string lastName = "Warner"; string formattedName = string.Format("Full Name: {0, -10} {1, 10}", firstName, lastName); Console.WriteLine(formattedName); // Output: Full Name: Smith Warner

In this example, we utilize formatting options for strings. The "{0,-10}" option aligns the first name to the left within a 10-character width, and "{1,10}" aligns the last name to the right within a 10-character width.


Using the Format method's rich set of formatting options and data type support, developers can create well-formatted and easily readable strings, catered to specific formatting needs in different scenarios like user interfaces, reports, logs, and more.