Code provider to format the String.Format call output
The String.Format call is the best approach for outputting information for the user to read. It replaces the format items in the specified template string with the string representation of the corresponding objects. The template string contains text that is desired in the final string and one or more format item placeholders, which will be replaced with other values passed as arguments to the String.Format call as either variable references or other literals.
When inserting values into format item placeholders, the values can be formatted by adding numerous format specifiers within the placeholders. A format specifier is an optional string of formatting codes. There are standard and custom format specifiers for formatting numbers, dates and times, and enumerations. Here are some of the available format specifiers:
Numeric Format Specifiers
Character |
Description |
Usage |
Example Output |
c | Currency | {0:c} | $12,345 |
d | Decimal (whole number) | {0:d} | 12345 |
e | Scientific | {0:e} | 1.234500e+004 |
f | Fixed Point | {0:f} | 12345 |
g | General | {0:g} | 12345 |
n | Thousand Separator | {0:n} | 12,345 |
r | Round Triple (decimal only) | {0:r} | System.FormatException |
x | Hexadecimal (int only) | {0:x4} | 3039 |
Date/Time Format Specifiers
Date and Time format specifiers are dependant on the user’s locale, so the output may be different.
Character |
Description |
Usage |
Example Output |
d | Short date | {0:d} | 04/09/2012 |
D | Long Date | {0:D} | 04 September 2012 |
t | Short time | {0:t} | 15:43 |
T | Long time | {0:T} | 15:43:55 |
f | Full Date/Time | {0:f} | 04 September 2012 15:43 |
F | Full Date/Time (long time) | {0:F} | 04 September 2012 15:43:55 |
g | General Date/Time | {0:g} | 04/09/2012 15:43 |
G | General Date/Time (long time) | {0:G} | 04/09/2012 15:43:55 |
M or m | Month Day | {0:M} | 04 September |
R or r | RFC1123 | {0:r} | Tue, 04 Sep 2012 15:43:55 GMT |
s | SortableDate/Time | {0:s} | 2012-09-04T15:43:55 |
u | Universal Full Date/Time | {0:u} | 2012-09-04 15:43:55 |
U | Universal Full Date/Time (long time) | {0:U} | 04 September 2012 15:43:55 |
Y or y | Year Month | {0:Y} | September 2012 |
O or o | Round-Trip Date/Time |
{0:O} | 2012-09-04T15:43:55.7770000 |
Custom Date/Time Format Specifiers
Format |
Description |
Usage |
Example Output |
dd | Day | {0:dd} | 04 |
ddd | Day Name | {0:ddd} | Tue |
dddd | Full Day Name | {0:dddd} | Tuesday |
f, ff, … | Second Fractions | {0:fff} | 777 |
gg, … | Era | {0:gg} | A.D. |
hh | 2 Digit Hour | {0:hh} | 03 |
HH | 2 Digit Hour, 24hr Format | {0:HH} | 15 |
mm | Minute 00-59 | {0:mm} | 43 |
MM | Month 01-12 | {0:MM} | 09 |
MMM | Month Abbreviation | {0:MMM} | Sep |
MMMM | Full Month Name | {0:MMMM} | September |
ss | Seconds 00-59 | {0:ss} | 46 |
tt | AM or PM | {0:tt} | PM |
yy | Year, 2 digits | {0:yy} | 12 |
yyyy | Year | {0:yyyy} | 2012 |
zz | Timezone offset, 2 digits | {0:zz} | -05 |
zzz | Full timezone offset | {0:zzz} | -05:00 |
: | Separator | {0:hh:mm:ss} | 15:43:55 |
/ | Separator | {0:dd/MM/yyyy} | 04/09/2012 |
General Format Specifiers
You can also use custom format strings, such as decimal placeholders, and leading and trailing characters. In these examples, we pass in 12345.12.
Character |
Description |
Usage |
Example Output |
0 | Zero Placeholder | {0:00.0000} | 12345.1200 |
# | Digit Placeholder | {0:(#).##} | (12345).12 |
. | Decimal Point | {0:0.0} | 12345.12 |
, | Thousand Separator | {0:0,0} | 12,345 |
% | Percent | {0:0%} | 1234512% |
e | Exponent Placeholder | {0:00e+0} | 12e+3 |
Some useful examples
Format | Usage | Example Output |
0:000-00-0000 | SSN | 012-34-5678 |
0:(###)###-#### | US Phone Number | (555)555-5555 |
0:#-(###)###-#### | US Phone Number | 1-(555)555-5555 |
0:1-(###)###-#### | US Phone Number | 1-(555)555-5555 |
As you can see there are too many of format specifiers to remember. But CodeRush may help you to format the template string without necessity to remember any of the format specifiers. The Format Item… code provider is available on the format item placeholder:
Once executed, it shows you the String.Formatter dialog where you can choose the format specifier you prefer and see the sample output before it is applied:
The dialog allows you to easily apply a format specifier to the template string. The corresponding format specifiers are enabled automatically based on the value type passed as argument into the String.Format call. The feature might be very helpful if you forgot any of the format specifiers and want to apply a custom string output.
The String.Formatter has been built quickly in front of the users in the CodeRush Feature Workshop Webinar series. You might want to review them to learn more on how to build great features quickly. There are two parts:
- CodeRush Feature Workshop: Improving String.Format (Part 1)
- CodeRush Feature Workshop: Improving String.Format (Part 2)