Reader Level:
String in C#

Format String in C#

By Mahesh Chand on Aug 21 2010
This code snippet shows how to format strings in C#.
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 15.8k
  • 0

Formatting Strings

You can use the Formatmethod to create formatted strings and concatenate multiple strings representing multiple objects. The Format method automatically converts any passed object into a string.

For example, the following code uses integer, floating number and string values and format them into a string using the Format method.

Listing 1: Using Format method to format a string

val = 7;
name = "Mr. John";
num = 45.06f;
str = String.Format ("Days Left : {0}. Current DataTime: {1:u}. \n String: {2}, Float: {3}" , val, DateTime.Now, name, num);

The output of Listing 1 is shown Figure 1.


Figure 1.

Read C# Strings  to learn more about strings in C#.