Article C0008 C# .NET
String.Split() / String.Format()

One of the great strengths of the .NET framework is the String manipulation power. One of the great features is the String.Split() method, which I use very often. To separate a comma separated value line, separated by a semicolon, simply create a multistring and assign the split result:

    string[] split = csvLine.Split(‘;’);
You can simply check the number of separated values like this:

    if (split.Length != 4) MessageBox.Show(“Line must contain just 4 values!”);
For values not directly identified as string add the .ToString() method. After this method you can use the .Split() method or the one discussed next, the .Format() method. The String.Format() method is a powerful feature described on MSDN. Remember the old-school syntax of C-language, like printf(“%2d”,value), which printed a decimal value with two digits. A value of 2 will be printed as 02. Even more handy is the hexadecimal notation which simply can be printed like this: printf(“0x%2x”, value) or printf(“0x%2X”, value) when capitals are needed. A value of 10 will respectively result in 0x0a or 0x0A.

Most features are available within the C# language using a slightly different slang. The first example can be reached by doing the following:

    string result = String.Format(“{0:d2}”, value);
 
While the second is almost as simple as the C version and the one above:

    string result = String.Format(“0x{0:x2}”, value);
Notice the different way arguments are passed between C and C#. C is using the arguments in the same sequence as provided in the method call. Within C# arguments can be placed anywhere in the argument order and used more than once.

    print(“%d, %2d, %d”,1,2,3); /* results in 1, 02, 3 */
    
    String.Format(“{3}, {0}, {1}, {3:d2}”,1, 2, 3); // results in 3, 1, 2, 03
Here is a compilation of useful String.Format() information found on the Internet, updated for the .NET 3.5 framework.

Formatting Strings
The string padding / alignment options are shown here:

Padding/Alignment
Syntax Result
String.Format("--{0,10}--","text"); --      text--
String.Format("--{0,-10}--","text"); --text      --

Standard Numeric Formatting
Formatting numbers is culture specific. So when your regional settings dictate Dutch notation and the euro sign, one thousand euro will be shown like this € 1.000,- On the other hand, one thousand dollar will be shown like this $ 1,000.-.

Standard Numeric
SpecifierNameSyntaxResult (NL) on input:
(double)1.2345 (US)
Result (NL) on input:
(int)12345
C or cCurrency{0:c}€ 1,23€ 12.345,00
D or dDecimal{0:d}FormatException12345
E or eExponential{0:d}1,234500e+0001,234500e+004
F or fFixed-point{0:f}1,2312345,00
G or gGeneral{0:g}1,234512345
N or nNumber{0:n}1,2312.345,00
P or pPercent{0:p}123,45 %1.234.500,00 %
R or rRound-trip{0:r}1,2345FormatException
X or xHexadecimal{0:x}FormatException3039

Any other character not listed will result in a FormatException. With any of these examples the size of the String can be set to a minimum length. So the {0:c} written as {0:c4} with an input argument of 10 will result in € 10,0000. And a decimal written like {0:d4} with input 12 will result in 0012. As stated earlier, the only case-sensitive specifier is the hexadecimal identifier. An input of 10 will result in A when using {0:X} and will result in a when using {0:x}.

For country specific currency you’ll need to add the “using System.Globalization;” namespace. When using the regular method will work like this:

    lblVal.Text = String.Format("{0:c}", 10);
Will result in: € 10,00

The following, culture specific version, will work like this:


    int value=10;
    
    lblVal.Text=value.ToString("c", CultureInfo.CreateSpecificCulture("en-US"));
Will result in: $ 10.00

Additional information: MSDN.

Custom Number Formatting As shown within the standard numeric formatting the padding, in this example {0:d4} the value 4, can be used to specify the decimals width notation. The custom number formatting specifiers give mote granular control over the number formatting process.

Custom Numeric
SpecifierNameSyntaxResult (NL) on input:
(double)1234.56 (US)
0Zero placeholder{0:00.00}1234,560
#Digit placeholder{0:#.##}1234,56
.Decimal point{0:0.0}1234,6
,Thousand separator{0:0,0}1.235
%Percentage{0:0%}123456%
E0Scientific{0:###E+0}123E+1
E+0Scientific{0:###E+0}123E+1
E-0Scientific{0:###E+0}123E+1
\Escape characterUse to avoid escape sequence processing
'ABC'ScientificCharacters enclosed in single or
double quotes are copied to
the result string, and
do not affect formatting
"ABC"ScientificCharacters enclosed in single or
double quotes are copied to
the result string, and
do not affect formatting
;Section separator{0:##;(##)}On positive input: 12345
On negative input (12345)

Additional information: MSDN.

Standard Date and Time Formatting
Be aware of the fact that date and time values are extremely culture specific. The values within my example are Dutch (NL) based. The date and time formatting specifiers are shown here:

Standard Date and Time
SpecifierNameSyntaxResult (NL) on input:
DateTime(1968,07,12,7,35,00)
dShort date pattern{0:d}12-7-1968
DLong date pattern{0:D}vrijdag 12 juli 1968
fFull date/time (short time){0:f}vrijdag 12 juli 1968 7:35
FFull date/time (long time){0:F}vrijdag 12 juli 1968 7:35:00
gGeneral date/time (short time){0:g}12-7-1968 7:35
GGeneral date/time (long time){0:G}12-7-1968 7:35:00
m,MMonth day{0:m}12 juli
o,ORound-trip date/time{0:o}1968-07-12T07:35:00.0000000
r,RRFC1123{0:r}Fri, 12 Jul 1968 07:35:00 GMT
SSortable date/time (ISO8601){0:s}1968-07-12T07:35:00
tShort time{0:t}7:35
TLong time{0:T}7:35:00
uUniversal sortable date/time{0:u}1968-07-12 07:35:00Z
UUniversal full date/time{0:U}vrijdag 12 juli 1968 5:35:00
y,YYear month{0:y}juli 1968

Additional information: MSDN.

Custom Date and Time Formatting
Custom date and time formatting is extremely culture specific. The examples used here are based on Dutch (NL).

Custom Date and Time
SpecifierNameSyntaxResult (NL) on input:
DateTime(1968,07,12,7,35,00)
dDay of the month{0:d, M}12, 7
dDay of the month{0:d MMMM}12 juli
dd MMDay of month as number{0:dd MM}12 07
dddAbbreviated day{0:ddd d MMM}vr 12 jul
ddddFull name of the day of the week{0:dddd dd MMMM}vrijdag 12 juli
fMost significant digit of second fraction{0:hh:mm:ss.f}07:35:00.0
ffTwo most significant digits
of second fraction
{0:hh:mm:ss.ff}07:35:00.000
fff (up to 7)Three most significant digits
of second fraction
{0:hh:mm:ss.fff}1968-07-12T07:35:00.0000000
g, ggRepresents the period of the era{0:MM/dd/yyyy g}07-12-1968 A.D.
h or hhRepresents the hour from 1 to 12
or 01 to 12
{0:hh:mm:ss}07:35:00
H or HHRepresents the hour from 0 to 23
or 00 to 23
{0:HH:mm:ss}07:35:00
%KTime zone based on DateTime.Now{0:%K}+01:00
mMinute{0:mm}35
MMonth{0:M}12 juli
MMMonth as a number{0:MM}07
MMMAbbreviated name of the month{0:MMM}jul
MMMMFull name of the month{0:MMMM}juli
ssRepresents the seconds from 0 to 59{0:ss}00
tThe AM/PM designator{0:tt}A
(issue under Vista, no output)
ttThe AM/PM designator{0:hh:mm:ss tt}07:35:00 AM
(issue under Vista, no output)
yRepresents the year{0:yyyy}1968
z Represents the signed offset
of the local operating system's
time zone from Coordinated
Universal Time (UTC),
measured in hours
{0:%z} {0:zz} {0:zzz} +2 +02 +02:00


~Edward