Using Escape
|
|
SEQUENCE |
PURPOSE |
|---|---|
|
\n |
New line |
|
\r |
|
|
\r\n |
Carriage returnnew 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 ).
char newline = '\n'; char backslash = '\';
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 ).
//the \n character is ignored
Response.Write("first line
\n
second line");
//the \n character is preserved. To see it
//you need to set TextMode to Multiline
txtMessage.Text = "***first line
\n
second line***";
//<br> character translated to line break
Response.Write("first line
<br>
second line");
//<br> character printed verbatim
txtMessage.Text = "first line
<br>
second line";
Using Literal Strings
Table 4.1
showed a list of special characters. These characters are
To create a literal string:
|