How to escape the character \
in C#?
Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences.
In particular, the \n escape sequence represents the newline character. A \n in a printf format string tells awk to start printing output at the beginning of a newline.
ASCII escape character The ASCII "escape" character (octal: \033 , hexadecimal: \x1B , or ^[ , or, in decimal, 27 ) is used in many output devices to start a series of characters called a control sequence or escape sequence.
\ is a special character within a string used for escaping. "\" does now work because it is escaping the second " . To get a literal \ you need to escape it using \ .
You just need to escape it:
char c = '\\';
Or you could use the Unicode escape sequence:
char c = '\u005c';
See my article on strings for all the various escape sequences available in string/character literals.
You can escape a backslash using a backslash.
//String
string backslash = "\\";
//Character
char backslash = '\\';
or
You can use the string literal.
string backslash = @"\";
char backslash = @"\"[0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With