Jump to content

String and Characters in C#

0
  chco's Photo
Posted Aug 10 2010 01:10 PM

This excerpt from C# 4.0 Pocket Reference, Third Edition explains how to use strings and characters in C#. A list of escape sequence characters is given as well as a few ways to manipulate strings.
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:
CharMeaningValue
\'Single quote0x0027
\"Double quote0x0022
\\Backslash0x005C
\0Null0x0000
\aalert0x0007
\bBackspace0x0008
\fForm feed0x000C
\nNew line0x000A
\rCarriage return0x000D
\tHorizontal tab0x0009
\vVertical tab0x000B


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"));  // −1


Searching within strings

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.

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.

Cover of C# 4.0 Pocket Reference
Learn more about this topic from C# 4.0 Pocket Reference, 3rd Edition. 

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

Learn More Read Now on Safari


Tags:
0 Subscribe


0 Replies