Flylib.com

Books Software

 
 
 

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



Using Literal Strings

Table 4.1 showed a list of special characters. These characters are characters that must be represented using the escape character (\) . In C# it is also possible to create literal strings. Literal strings enable you to use special characters like the backslash or double quotes without having to use special codes or the escape character.

To create a literal string:

  1. Type string str where str is the name of the variable to hold the string.

  2. Type = @"the string" ; (the @ character followed by double quotes, followed by any string context, followed by another double quote) ( Figure 4.36 ).

    Figure 4.36 Literal strings enable you to represent special characters without using escape sequences.
    string lit =
    
    @
    
    "this is a boring literal string";
    

graphics/tick.gif Tips

  • You can use backslashes in literal strings without having to use two backslashes ( Figure 4.37 ).

    Figure 4.37 Because the backslash is used to represent escape sequences, to add a backslash you need to type two backslashes. However, with literal strings you don't use escape sequences, so you can use the backslash as a backslash.
    //non-literal string requires double backslash
    string windir1 = "c:\windows\system32\";
    
    //literal string can use a single backslash
    string windir2 =
    
    @"c:\windows\system32\"
    
    ;
    
  • To specify the double-quote character you can put two double-quote characters together ( Figure 4.38 ).

    Figure 4.38 The double-quote character is a problem no matter what type of string you use, because it tells the compiler where the string begins and ends. With literal strings to use a double quote as part of the string all you have to do is put two double quotes together.
    //two double quotes are turned into one
    //literal double quote
    string sql=
    @"select * from contacts where "
    sql += @"lastname=
    
    ""
    
    Jones
    
    ""
    
    sql += @"and firstname=
    
    ""
    
    Indiana
    
    ""
    
    ;
    
  • If you use literal strings you can split a string into multiple lines of code; however, keep in mind that doing so inserts a carriage return character into the string ( Figure 4.39 ).

    Figure 4.39 When you break a literal strings into multiple lines the carriage returns you put in the editor are preserved. However, they aren't visible in HTML because white space is condensed into a single space.
    
    string msg = @"Welcome to WidgetsUSA
    
    
    First Bank of Cyberspace.
    
    
    Your Balance is 00.";
    
    txtMessage.Text = msg;
    //output:
    // Welcome to WidgetsUSA
    //           First Bank of Cyberspace.
    //           Your Balance is 00.
    
    Response.Write(msg);
    //output:
    //Welcome to WidgetsUSA First Bank of
    Cyberspace. Your Balance is 00.
    
  • The rules for when white space is preserved, which we discussed in the previous section, also apply to literal strings.