Reader Level:
ARTICLE

Format String in C#

Posted by Mahesh Chand Articles | String in C# August 21, 2010
This code snippet shows how to format strings in C#.
  • 0
  • 0
  • 9349

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

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


The output of Listing 1 is shown Figure 1.

FS1.gif

Figure 1.



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

COMMENT USING

Trending up