You need to format one or more embedded pieces of information inside of a string, such as a number, character, or substring.
The
static string.Format
method allows you to format
strings in a variety of ways. For example:
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
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:
index
A number identifying the zero-based position of the
section’s data in the
args
parameter array. The data is to be
formatted accordingly and substituted for this section. This number
is required.
alignment
The number of spaces to insert before or after this data. A negative number indicates left justification (spaces are added to the right of the data), and a positive number indicates right justification (spaces are added to the left of the data). This number is optional.
formatString
A string indicating the type of formatting to perform on this data. This section is where most of the formatting information usually resides. Tables Table 2-2 and Table 2-3 contain valid formatting codes that can be used here. This part is optional.
Table 2-2. The standard formatting strings
Table 2-3. Custom formatting strings
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 in Table 2-3. 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.