String.Format()

String.Format is a powerful formatting function which allows both the concatenation of a set of arguments and the formatting of non-string parameters. The function uses placeholders in a template string to produce the results. Each placeholder can have an optional format attached to it.

String.Format("Hello {0}, today is {1:d}.", "Fred", New DateTime(2006, 4, 16))
"Hello Fred, Today is 16 April 2006."

The format can be arranged in three parts representing the format for positive, negative and zero numbers.

String.Format("{0:0;(0);Nothing}", -123)
"(123)"

Finally, the string can be aligned.

String.Format("{0,10}", "Hello")
"     Hello"
String.Format("{0,-10}", "Hello")
"Hello     "
String.Format("{0,10:0.0}", 99)
"      99.0"

The formatting of the arguments is controlled by the formatting string. This can either be a standard format, which is normally just a single letter giving one of the well defined formats for the datatype, or a custom format, built using a string of formatting elements.

Numbers

Standard Formats

Format Character Effect Example
c Currency 1234.567 £1,234.57
e Exponent (Scientific) 1234.567 1.234567e+003
f Fixed point 1234.567 1234.57
g General 1234.567 1234.567
n Number 1234.567 1,234.567
r Round Trip 1234.567 1234.567
x Hexadecimal 1234 4d2
p Percentage 1234.567 123,456.70 %

Custom Formats

Format Character Effect Example
0 Required digit placeholder 12.3 0.000 12.300
# Optional digit placeholder
. Decimal point placeholder
, Thousand separator
% Percentage

Dates

Standard Formats

Format Character Effect Example (1 January 2001 08:04:09)
d Short Date 01/01/2001
D Long Date 01 January 2001
t Short Time 08:04
T Long Time 08:04:09
f Full Short Date/Time 1 January 2001 08:04
F Full Long Date/Time 1 January 2001 08:04:09

Custom Formats

Format Character Effect Example (1 January 2001 14:04:09)
yy Year {0:yy} 01
{0:yyyy} 2001
M Month {0:M}* 1
{0:MM} 01
{0:MMM} Jan
{0:MMMM} January
d Day {0:d}* 1
    {0:dd} 01
    {0:ddd} Mon
    {0:dddd} Monday
h Hour {0:h}* 2
    {0:hh} 02
    {0:H}* 14
    {0:HH} 14
m Minute {0:m}* 4
    {0:mm} 04
s Second {0:s}* 9
    {0:ss} 09
tt AM/PM {0:tt} AM
    {0:t}* A

*Some of the formats will actually display the data in a different format if used alone (e.g. {0:d} will display the short date format), but when used with any other formatting character they produce the output show above. If you want to use a single character as a format string, preceed it with % (e.g. {0:%d})

Enums

Format Character Effect Example (System.ConsoleColor.Green)
G or g, F or f Display as String Green
D or d Display as Integer 10
X or x Display in Hex 0000000A

Examples

output = String.Format("Console colour codes:{0}{0}{1,-10:f}{1:d}{0}{2,-10:f}{2:d}", Environment.NewLine, ConsoleColor.Red, ConsoleColor.Green)

Console colour codes:

Red       12
Green     10

output = String.Format("Today is {0:dddd}, the {0:%d} of {0:MMMM yyyy}. The time is {0:h:mm tt}", DateTime.Now)

Today is Monday, the 6 of February 2012. The time is 8:29 PM

Custom .NET Training ASP.NET Training Silverlight Training LINQ Training IT Support