Friday, January 20, 2023

C# All valid string escape sequences

C# defines the following character escape sequences:

Strings - C# Programming Guide | Microsoft Learn

String escape sequences

Escape sequenceCharacter nameUnicode encoding
\'Single quote0x0027
\"Double quote0x0022
\0Null0x0000    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.
\fForm feed0x000C
\nNew line0x000A
\rCarriage return0x000D
\tHorizontal tab0x0009
\vVertical tab0x000B
\uUnicode escape sequence (UTF-16)\uHHHH (range: 0000 - FFFF; example: \u00E7 = "ç")
\UUnicode escape sequence (UTF-32)\U00HHHHHH (range: 000000 - 10FFFF; example: \U0001F47D = "👽")
\xUnicode escape sequence similar to "\u" except with variable length\xH[H][H][H] (range: 0 - FFFF; example: \x00E7 or \x0E7 or \xE7 = "ç")


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.


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\";


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

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