Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write the escape char '\' to code

Tags:

c#

escaping

How to escape the character \ in C#?

like image 328
Stav Alfi Avatar asked Apr 01 '13 17:04

Stav Alfi


People also ask

How do you write a character escape?

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.

What is the '\ n escape character?

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.

How do you escape ASCII?

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.

How do I escape a character from a string?

\ 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 \ .


2 Answers

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.

like image 95
Jon Skeet Avatar answered Oct 17 '22 04:10

Jon Skeet


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];
like image 12
Dustin Kingen Avatar answered Oct 17 '22 03:10

Dustin Kingen