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 = '\n'; char backSlash = '\\';
The escape sequence characters are:
| Char | Meaning | Value |
| \' | Single quote | 0x0027 |
| \" | Double quote | 0x0022 |
| \\ | Backslash | 0x005C |
| \0 | Null | 0x0000 |
| \a | alert | 0x0007 |
| \b | Backspace | 0x0008 |
| \f | Form feed | 0x000C |
| \n | New line | 0x000A |
| \r | Carriage return | 0x000D |
| \t | 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:\t";
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 = @"\\server\fileshare\helloworld.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";
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
Since string is immutable, using the + operator repeatedly to build up a string can be inefficient. The solution is to instead 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")); // −1Searching within strings
string’s indexer returns a character at a specified position:
Console.Write ("word"[2]); // rThe IndexOf/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.
When you're programming C# 4.0 and need a little help, this tightly focused and practical book tells you exactly what you need to know -- without long introductions or bloated examples. It's ideal as a succinct quick reference or as a guide to get you rapidly up to speed if you already know Java, C++, or an earlier version of C#. Written by the authors of the acclaimed C# 4.0 in a Nutshell (O’Reilly), this book covers the entire C# 4.0 language -- without skimping on the details




Help






