- Preparing Your Machine to Work with Strings
- Working with Strings
- Initializing Strings
- Comparing Strings
- Concatenating Strings
- Finding the String Length
- Comparing and Concatenating Strings in the Sample Application
- Creating Strings from Characters
- Using Escape Characters
- Using Literal Strings
- Accessing the String's Characters
- Finding a Substring within a String
- Extracting Part of the String
- Splitting a String
- Joining a String
- Uppercasing and Lowercasing
- Formatting Strings
- Finishing the Sample Application
- Representing Objects as Strings
- Allocating Strings with StringBuilder
Using Escape Characters
A number of characters have been labeled as special characters. Take for example, the quote character. How would you represent a quote within a string, since the compiler interprets a quote as the beginning or end of the string? Whenever you want to use one of these special characters you need to use a technique known as escaping the character. Escaping the character means typing a backslash (\) in front of the character (Figure 4.33).
Figure 4.33 Escape characters have been a part of languages like C and C++ since the beginning of time. In C# they are used also to represent line breaks, quotation marks, and other special characters.
To use special characters in your string:
Type a backslash \ before the special character.
Enter one of the characters from Table 4.1.
Table 4.1. Escape Sequences (Special Characters in C# Strings)
SEQUENCE |
PURPOSE |
\n |
New line |
\r |
Carriage return |
\r\n |
Carriage return—new line |
\" |
Quotation marks |
\\ |
Backslash character |
\t |
Tab |
Tips
Even though escaped characters involve two characters (backslash \ plus the escaped character) the compiler treats the sequence as one character. That means that \n is a single character and the same is true for \", \\, etc (Figure 4.34).
Figure 4.34 It may look strange, but even though you have to type a couple of characters to represent an escaped character, each escape sequence is treated as a single character.
There are two main ways of outputting text to a Web client. You can set the text property of a control like a label or a textbox, or you can use a command like Response.Write. When you use Response.Write to output text, the browser treats the text as HTML. In HTML spaces, tabs, and carriage returns are known as white space. In HTML, if you have multiple spaces or a tab, the characters are converted to a single space, and carriage returns are ignored. That means that characters like \n are just ignored. This also happens when you set the text of a label, because label Text is outputted to the browser as HTML. However, when you set the text of a textbox the characters are displayed as raw text and the white space is preserved (Figure 4.35).
Figure 4.35 Carriage returns, tabs, and spaces are treated as white space in HTML. In HTML all white space is condensed into a single character. To break lines you have to use HTML tags such as <br>.