Strings and Characters

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

	char c = 'A';         // simple character

Escape sequencesexpress 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 outlined below.

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

Char Conversions

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

stringis a reference type, not a value type. Its equality operators, however, implement value-type semantics.

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

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

The cost being 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:

	string escaped  = "First Line
Second Line";
	string verbatim = @"First Line
	Second Line";

	Console.WriteLine (escaped == verbatim); // True

You can include the double-quote character in a verbatim literal by writing it twice:

	string xml = @"<customer id=""123""></customer>";

String concatenation

The + operator concatenates two strings:

	string s = "a" + "b";

The righthand operand may be a non-string value, in which case ToString is called on that value. For example:

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

Because string is immutable, using the + operator repeatedly to build up a string can be inefficient. The solution is to instead use the System.Text.String Builder type—this represents a mutable (editable) string, and it 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 CompareTomethod, 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 Index Of/Last Index Of methods search for a character within the string; the Contains, StartsWith, and EndWith 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