Sabari M Sabari M
Updated date Aug 24, 2020
String.Format method is used to converts the value of objects into strings based on the formats mentioned and inserts them into another string.

String.Format method converts the value of objects into the strings and inserts the format mentioned into another string. To use String.Format method, you need to insert the value variable or expression into another string.

A single object in a String:

For example, you can insert the value of an integer value into a string to display it to the screen as a one-string:

int carCount = 4;
string str = String.Format("I have only {0} car(s).", carCount);
Console.WriteLine(str);
// Output: 
// I have only 4 car(s).

Multiple objects in a String:

Below example shows how to use two formats and two objects in a single string:

int carCount = 4;
string bikeCount = 2;
string str = String.Format("I have {0} cars and {1} bikes.", carCount, bikeCount);
Console.WriteLine(str);
// Output:
// I have 4 cars and 2 bikes.

Format using String.Format in a String:

Format String, based on the format specifier using string.format() method, For example, {0:d} applies the "d" format string to the first object in the object list. Below example you can see a single object and two format items: 

string str = String.Format("Today's date is {0:d} and the current time is {0:t}", DateTime.Now);
Console.WriteLine(str);
// Output
// Today's date is 8/06/2020 and the current time is 09:15 PM

Spacing and Alignment:

The below example shows an 8-character field to hold the string "Company" and some company strings, as well as a 13-character field to hold the string "Models" and some Models data. All the characters are right-aligned in the field.

// String.Format("{index[,alignment][:formatString]}", object);

string[] cars = { "Ford", "Toyota", "Audi", "Honda", "BMW" };
string[] models = { "Ecosport", "Camry", "A6", "City", "X7" };
var str = new System.Text.StringBuilder();
str.Append(String.Format("{0,8} {1,13}\n\n", "Company", "Models"));
for (int index = 0; index < cars.Length; index++)
{
	str.Append(String.Format("{0,8} {1,13}\n", cars[index], models[index]));
}                
Console.WriteLine(str);

// Output
// Company         Models
//
//    Ford       Ecosport
//  Toyota          Camry
//    Audi             A6
//   Honda           City
//     BMW             X7

Other Usage of String.Format Methods:

// String Format using Decimal Numbers
string.Format("{0:0.##}", 1234.5)      // 1234.5
string.Format("{0:0.##}", 1234.0)      // 1234
// String Format using DateTime
string.Format("{0:dd-MM-yyyy}", dt)                                  // 12/06/2020
string.Format("{0:MMM dd, yyyy}", dt)                                // Jun 12, 2020
string.Format("{0:dddd, dd MMMM yyyy H:mm}", dt)                     // Friday, 12 June 2020 16:06
// String Format Using Currency
// "C0" - No decimal values
string.Format("{0:C0}", 1456.12155)   // $1,456
// "C1" - Only one decimal value
string.Format("{0:C1}", 1456.12155)   // $1,456.1
// "C2" - Two decimal values
string.Format("{0:C2}", 1456.12155)   // $1,456.12

More details about Decimal, DateTime and Currency Formats, please visit below blogs,

ABOUT THE AUTHOR

Sabari M
Sabari M

IT professional with 14+ years of experience in Microsoft Technologies with strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices, V...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!
×