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