Strings and Characters

C#’s char type (aliasing the System.Char type) represents a Unicode character and occupies two bytes. A char literal is specified inside single quotes:

char c = 'A';       // Simple character

Escape sequences express characters that cannot be expressed or interpreted literally. An escape sequence is a backslash followed by a character with a special meaning. For example:

char newLine = '
';
char backSlash = '';

The escape sequence characters are:

Char

Meaning

Value

'

Single quote

0x0027

"

Double quote

0x0022

\

Backslash

0x005C

Null

0x0000

a

Alert

0x0007



Backspace

0x0008

f

Form feed

0x000C

New line

0x000A

Carriage return

0x000D

Horizontal tab

0x0009

v

Vertical tab

0x000B

The u (or x) escape sequence lets you specify any Unicode character via its four-digit hexadecimal code:

char copyrightSymbol = 'u00A9';
char omegaSymbol     = 'u03A9';
char newLine         = 'u000A';

An implicit conversion from a char to a numeric type works for the numeric types that can accommodate an unsigned short. For other numeric types, an explicit conversion is required.

String Type

C#’s string type (aliasing the System.String type) represents an immutable sequence of Unicode characters. A string literal is specified inside double quotes:

string a = "Heat";

Note

string is a reference type, rather than a value type. Its equality operators, however, follow value-type semantics:

string a = "test", b = "test";
Console.Write (a == b);  // True

The escape sequences that are valid for char literals also work inside strings:

string a = "Here's a tab:	";

The cost of this is that whenever you need a literal backslash, you must write it twice:

string a1 = "\\server\fileshare\helloworld.cs";

To avoid this problem, C# allows verbatim string literals. A verbatim string literal is prefixed with @ and does not support escape sequences. The following verbatim string is identical to the preceding one:

string a2 = @"\serverfilesharehelloworld.cs";

A verbatim string literal can also span multiple lines. You can include the double-quote character in a verbatim literal by writing it twice.

String concatenation

The + operator concatenates two strings:

string s = "a" + "b";

One of the operands may be a nonstring value, in which case ToString is called on that value. For example:

string s = "a" + 5;  // a5

Using the + operator repeatedly to build up a string can be inefficient: a better solution is to use the System.Text.StringBuilder type—this represents a mutable (editable) string, and has methods to efficiently Append, Insert, Remove, and Replace substrings.

String comparisons

string does not support < and > operators for comparisons. You must instead use string’s CompareTo method, which returns a positive number, a negative number, or zero, depending on whether the first value comes after, before, or alongside the second value:

Console.Write ("Boston".CompareTo ("Austin"));   // 1
Console.Write ("Boston".CompareTo ("Boston"));   // 0
Console.Write ("Boston".CompareTo ("Chicago"));  // -1

Searching within strings

string’s indexer returns a character at a specified position:

Console.Write ("word"[2]);   // r

The IndexOf and LastIndexOf methods search for a character within the string. The Contains, StartsWith, and EndsWith methods search for a substring within the string.

Manipulating strings

Because string is immutable, all the methods that “manipulate” a string return a new one, leaving the original untouched:

  • Substring extracts a portion of a string.

  • Insert and Remove insert and remove characters at a specified position.

  • PadLeft and PadRight add whitespace.

  • TrimStart, TrimEnd, and Trim remove whitespace.

The string class also defines ToUpper and ToLower methods for changing case, a Split method to split a string into substrings (based on supplied delimiters), and a static Join method to join substrings back into a string.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.160.181