[C#] Understanding of escape characters

Zero, why are you confused about escape characters?

(1) There are too many types of it, many of which are not commonly used and feel unfamiliar

(2) Escape the escape characters (including the use of @)

(3) Escape characters automatically added by the system (such as newline?)


The results of the study are as follows :

In fact, all programming languages ​​have escape characters for basically two reasons:

1: Convert ordinary characters to special purposes, usually in programming languages, to represent characters that cannot be displayed directly, such as the back key, the enter key, and so on. ( There are some non-printable characters in ASCII, such as line feed, bell, etc. These characters must be directly written into ASCII values ​​before they can be output. There is no rule between these ASCII characters, the readability is not high, and it is difficult to remember. For this reason, people invented Escape characters to replace ASCII values ​​to get rid of the shortcomings of ASCII and facilitate people's use ) For example:

\0 Null
\a Bell
\b Backspace
\f Feed form feed 
\n Line feed
\r Enter 
\t Horizontal jump to the next tab position
\v Vertical tab (vertical tab)

2: Used to convert characters with special meaning back to their original meaning. Usually used in regular expressions. E.g:

\' single quote character 
\" single quote character 

\\ backslash "\" 


The special @ sign in C#: As long as the @ sign is added before the statement, it means that all the escape characters after the @ sign are not escaped. This is because if you want to output and escape related characters, it will be troublesome.

For example: to print: "\t", then you need to write the code like this; Write(" \" \\ t \" ");

And using the @ sign, you can write: @Write(" "\t" ");

Test in Unity:

1. Copy a large piece of text without paragraph marks in word into the script and display it as one line.

2. Copy a large piece of text with paragraph marks added in word to the script, and the place where there is a newline is also wrapped in the script, and it becomes red.

   If you add @ in front, it will not be red, and at the same time, the place where the line wraps in the script will also wrap when displayed.

3. In the C# script, directly hit the enter key in a line of text. After the line break, vs automatically helps me make them into two strings.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324703308&siteId=291194637