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 |
---|---|---|
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
|
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.
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";
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.
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
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
string
’s indexer returns a character at a specified position:
Console.Write ("word"[2]); // r
The IndexOf
/LastIndexOf
methods search for a character
within the string; the Contains
,
StartsWith
and EndsWith
methods search for a substring within the string.
Because string
is immutable,
all the methods that “manipulate” a string return a new one, leaving
the original untouched:
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.