C#. The escape characters in net

Source: Internet
Author: User

When declaring a string variable, there are some characters that cannot be included in the variable in the usual way. To solve this problem, C # provides two different approaches.

The first method is to use ' escape sequences '. For example, we want the following string

"Hello World

How is You "

We can declare the string using the following statement: string a = "\" Hello world\nhow is You\ "". An escape sequence of "and newline characters" is used in this statement. Escape sequences for more characters can be found in the following table:

Character

Escape Sequence

\‘

"

\"

\

\\

Alerts

\a

Backspace

\b

Page break

\f

Line break

\ n

Carriage return character

\ r

Tab character

\ t

Vertical Tab Character

\v

Use numeric-specified Unicode characters, such as \u2000

\u

Unicode characters, such as \xc8, that are specified using hexadecimal numbers

\x

Null value

(zero

The second method is to use ' verbatim string ' literals. This method places the string you want to get between @ "and". If we need to assign the C:\My documents\ to ' path ', we can use the escape sequence method: String path = "C:\\My documents\\", or you can use the following statement: string path = @ "\ n Mydocuments\ ".

Strings obtained by using the latter method can also span multiple lines without the need to use ' \ n '. Using this method, the only string that needs to be used into the escape sequence is ", with the escape character" "(two double quotation marks that are joined together). For example, if you want the word "big" contains three letters. Assignment to ' text ', we can use the following statement: string text = @ "The word" "Big" "contains three letters." 。

C#. The escape characters in net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.