The static string.Format method allows you to format strings in a variety of ways, this is very helpful when you need to output the specific format to the display.
The string.Format method allows a wide range of formatting options for string data. The first parameter of this method can be passed a string that may look similar to the following:
"The item ID = {0,5:G}"
The text The item ID = will be displayed as is, with no changes. The interesting part of this string is the section enclosed in braces. This section has the following form:
{index, alignment:formatString}
The section can contain the following three parts:
|
The standard formatting strings
|
Custom formatting strings
|
The following examples demonstrate how to use the methods:
int ID = 12345; double weight = 12.3558; char row = ''Z''; string section = "1A2C"; string output = string.Format(@"The item ID = {0:G} having weight = {1:G} is found in row {2:G} and section {3:G}", ID, weight, row, section); Console.WriteLine(output); output = string.Format(@"The item ID = {0:N} having weight = {1:E} is found in row {2:E} and section {3:E}", ID, weight, row, section); Console.WriteLine(output); output = string.Format(@"The item ID = {0:N} having weight = {1:N} is found in row {2:E} and section {3:D}", ID, weight, row, section); Console.WriteLine(output); output = string.Format(@"The item ID = {0:(#####)} having weight = {1:0000.00 lbs} is found in row {2} and section {3}", ID, weight, row, section); Console.WriteLine(output);
The output is as follows:
The item ID = 12345 having weight = 12.3558 is found in row Z and section 1A2C The item ID = 12,345.00 having weight = 1.235580E+001 is found in row Z and section 1A2C The item ID = 12,345.00 having weight = 12.36 is found in row Z and section 1A2C The item ID = (12345) having weight = 0012.36 lbs is found in row Z and section 1A2C
To simplify things, the string.Format method could be discarded and all the work could have been done in the System.Console.WriteLine method, which calls string.Format internally, as shown here:
Console.WriteLine(@"The item ID = {0,5:G} having weight = {1,10:G} " +
"is found in row {2,-5:G} and section {3,-10:G}",
ID, weight, row, section);
The output of this WriteLine method is:
The item ID = 12345 having weight = 12.3558 is found in row Z and section 1A2C
In addition to the string.Format and the Console.WriteLine methods, the overloaded ToString instance method of a value type may also use the previous formatting characters. Using ToString, the code would look like this:
float valueAsFloat = 122.35; string valueAsString = valueAsFloat.ToString("[000000.####]");
The valueAsString variable would contain the formatted number contained in valueAsFloat. The formatted number would look like this:
[000122.35]
The overloaded ToString method accepts a single parameter of type IFormatProvider. The IFormatProvider provided for the valueAsFloat.ToString method is a string containing the formatting for the value type plus any extra text that needs to be supplied.
Popularity: 3% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply