Professional Documents
Culture Documents
Composite formatting provides an easy way to format strings, by first supplying a format string to
define the output format. For example, the following string “Your full name is {0} {1}.” identifies
two indexed placeholders for data insertion in the string. Each inserted parameter corresponding to
the index value of the parameter list. Note that the index value begins at zero, and the placeholders
within the format string need not appear in order. The following is an example of a write statement:
Console.WriteLine(“Your full name is {1}, {0}”, sFirstName, sLastName);
To encode an argument in C# string, type an open brace “{“, followed by parameter number, may
provide optional formation and width information, and end the sequence with a closing brace “}”.
You my follow the argument with an optional field width and formatting string in the form:
{N,W:Fp} Where N is the argument number, W is the field width, and F
is the format string with precision “p”, as shown in the above table.
Examples: WriteLine(“{0,0:c}”,3); $3.00
WriteLine(“{0,0:d3):{1}”,i,(char)i); 065:A
Some characters cannot be placed directly into the string code and require special handling. These
characters are prefixed with a backslash ‘\’ followed by a special character code. Thus, an escape
sequence is the combination of the backslash and special character code. For example, ‘\n’
represents a new line, and ‘\b’ represents backspace. For a single backslash, use ‘\\’.
EE364 Object Oriented Programming with C# Page 2/2
\\ Backslash 0x005C
\0 Null 0x0000
\b Backspace 0x0008
Unicode is an international (16-bit) standard for representing characters found in the majority
of human languages. It provides computer systems with functionality for building localized
applications that display the appropriate language and culture characteristics for different cultures.
Unfortunately, not all Unicode characters are available within a 16-bit character, thus some Unicode
characters are composed of surrogate character pairs totalling 32-buts. You can represent any
character using Unicode encoding, by prefixing the Unicode with \u.
In C#, you can use the “@ symbol” (verbatim string literal) in front of a string to signify that a
backslash should not be interpreted as the beginning of an escape sequence. Nothing is interpreted
after the “@” string syntax, including white spaces (backslash, new line and indentation).
Console.WriteLine(@“The director is C:\Program Files”);