The substitution method of the literal character in the string variable

Source: Internet
Author: User

1, usually directly write the string has the escape character is preceded by "@" can solve:

such as: string str = @ "Please confirm the data is wrong!" \ r \ n Interrupt program operation. ";

The actual deposit in STR is "Please confirm the data is wrong!" \\r\\n interrupt the program to run. "

2. What if the contents of the string variable are directly carried with the escape character?

For example, if STR is already in existence, "Please confirm the data is wrong!" \ r \ n Interrupt program operation. "Content.

At this point, the actual contents of the Str in the \ R and \ n is the escaped content, not the "\ r" or "\ n" character.

So if you want to change \ r or \ n to \\r or \\n, you will find the value after escaping, but the escaped value is often not a display character.

3, first to understand the escaped character in C # expression method.

The ASCII code can certainly represent this escaped non-display character, which is expressed in C # as follows:

Char Hex Oct Dec C # Presentation method

such as: Carriage return CR 0D 015 x0d

Line break LF 0A 012 x0a

BACKSPACE bar BS 010 8 x08

ESC key ESC 1 B 033 x1b

4. Now to replace the \ R and \ n in str with \ R and \\n:

After escaping the \ R is: \x0d escaped after \ n is: \x0a

String strtemp = str. Replace ("\x0a", "\\n"). Replace ("\x0d", "\\r");

The content in the result strtemp is "Please confirm the data is wrong!" \\r\\n interrupt the program to run. "

The substitution method of the literal character in the string variable

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.