C# defines the following character escape sequences:
String escape sequences
Escape sequence | Character name | Unicode encoding |
---|---|---|
\' | Single quote | 0x0027 |
\" | Double quote | 0x0022 |
\\ | Backslash | 0x005C |
\0 | Null | 0x0000 In .NET there is no special meaning to this character. It does not represent end of string as in C or C++. See .NET internal storage code share. |
\a | Alert | 0x0007 |
\b | Backspace | 0x0008 |
\f | Form feed | 0x000C |
\n | New line | 0x000A |
\r | Carriage return | 0x000D |
\t | Horizontal tab | 0x0009 |
\v | Vertical tab | 0x000B |
\u | Unicode escape sequence (UTF-16) | \uHHHH (range: 0000 - FFFF; example: \u00E7 = "ç") |
\U | Unicode escape sequence (UTF-32) | \U00HHHHHH (range: 000000 - 10FFFF; example: \U0001F47D = "👽") |
\x | Unicode escape sequence similar to "\u" except with variable length | \xH[H][H][H] (range: 0 - FFFF; example: \x00E7 or \x0E7 or \xE7 = "ç") |
Warning
When using the \x
escape sequence and specifying less than 4 hex digits, if the characters that immediately follow the escape sequence are valid hex digits (i.e. 0-9, A-F, and a-f), they will be interpreted as being part of the escape sequence. For example, \xA1
produces "¡", which is code point U+00A1. However, if the next character is "A" or "a", then the escape sequence will instead be interpreted as being \xA1A
and produce "ਚ", which is code point U+0A1A. In such cases, specifying all 4 hex digits (e.g. \x00A1
) will prevent any possible misinterpretation.
Note
If a character that does not appear in the table above is preceded with a backslash, the character value is returned. For example, if the compiler sees \x it will be treated as x.
Try to embed all those backslashes is error prone. To avoid this problem, you can use verbatim string literals. Verbatim are start with a @"..." and escaped characters will be ignored! Therefore printing @"\r\n" will print \r\n.
Verbatim string literals are more convenient for multi-line strings, strings that contain backslash characters, or embedded double quotes. Verbatim strings preserve new line characters as part of the string text. Use double quotation marks to embed a quotation mark inside a verbatim string.
string string filePath = @"C:\Users\Bond007\Documents\";
Related
To cut and paste all control characters into your favorite editor see the following post
Metadata Consulting [dot] ca: Notepad++ Control Characters Explained NUL, SOH, STX, etc
.NET Internal Storage - Examine Verbatim strings
In fact, you can look into the .NET internal storage of a string using the following code, and it rather expensive and slow to do. But in case you need it, especially when dealing with end of line values '\0' that have no meaning in .NET.
No comments:
Post a Comment