Reputation: 119
How to write escape character "\" to string?
Upvotes: 5
Views: 56424
Reputation: 88786
In a normal string, you use \\
If this is in a regular expression, it needs to be \\\\
The reason RegEx needs four is because both the String parser and RegEx engine support escapes. Therefore, \\\\
is parsed to \\
by the String parser then to a literal \
by the RegEx parser.
Upvotes: 13
Reputation: 454940
You can write:
String newstr = "\\";
\
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 \
.
Upvotes: 0
Reputation: 7712
string = @"c:\inetpub\wwwroot\"
is the same as
string = "c:\\inetpub\\wwwroot\\"
in some cases... i'm not 100% sure but i think it might be language dependant... i KNOW "@" works in C#
Upvotes: 0
Reputation: 27596
You escape certain characters by adding "\" in front of the character.
So \\
will work for you.
Upvotes: 0