You are on page 1of 2

EE364 Object Oriented Programming with C# Page 1/2

Composite String Formatting

1.1 String Formatting

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);

Character Description Format

C or c Local currency format Use local currency character format.

D or d Integer Precision pads the field with leading zeros.

E or e Scientific Precision sets the number of decimal places.

F or f Fixed Point Precision sets the number of decimal places.

G or g General Uses the compact form of E or F formatting.

N or n Number Format with embedded commas.

X or x Hexadecimal Format as hex-number.

R Round-Trip Formatting Returns

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

1.2 Escape Sequence

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

Escape Sequence Character Name Unicode Encoding

\’ Single quote 0x0027

\” Double quote 0x0022

\\ Backslash 0x005C

\0 Null 0x0000

\a Alert (System beep) 0x0007

\b Backspace 0x0008

\f Form feed 0x000C

\n New line 0x000A

\r Carriage return 0x000D

\t Horizontal tab 0x0009

\v Vertical tab 0x000B

\uxxxx Unicode character in hex Example: \u0029

\x[n] [ n] [ n]n Variable length Unicode Example: \x3A 20

\Uxxxxxxxx Unicode for surrogate pairs (4 bytes) Example: \UD840AC01

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.

1.3 Verbatim String Literal

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”);

Only the double quotes (“”) is supported by the verbatim string.

You might also like