Using Escape Characters


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.
 string letter = "Dear Mr. Jones,  \n\t  " letter += "The purpose of this letter "; letter += "is to discuss your "; letter += "so-called  \"  work  \"  in this " letter += "company."; /* shows the following text: Dear Mr. Jones,      The purpose of this letter is to discuss your so-called "work" in this company. */ 

To use special characters in your string:

  1. Type a backslash \ before the special character.

  2. 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 returnnew line

\"

Quotation marks

\\

Backslash character

\t

Tab

graphics/tick.gif 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.
     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 ).

    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>.
     //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"; 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net