Reputation: 1705
I'm trying to set the value of a string to something that has a \
in it, but cannot do so as they say I have an unrecognized escape sequence. Is it possible to write \
in a string?
Upvotes: 0
Views: 6628
Reputation:
All the above answers are right. I want to include one more way of doing the same i.e. by using a unicode character. e.g. the \u005c represents "\"
hence "hello \u005c world"; will give the output as hello \ world
All the below will give the same result
string test1 = "hello \\ world";
string test2 = @"hello \ world";
string test3 = "hello \u005c world";
For a list of unicode character set visit this site
Thanks
Upvotes: 3
Reputation: 1195
Alternatively, you can prefix the string with @
, which will tell the compiler to interpret the string literally.
string str = @"i am using \ in a string";
Upvotes: 1
Reputation: 2721
You must escape it... if you are using a regular string you must double the slash "hello\\world"
or if you want it as a literal you can use @"hello\world"
Upvotes: 5
Reputation: 1418
like others have pointed out, use double slash "\\"
OR you can change your string to a string literal, and not have to update your slashes...
eg
string a = @"some s\tring wi\th slashes";
Upvotes: 1
Reputation: 7892
Yes, use "\\".
For an explanation and a list of possible escape symbols, see http://msdn.microsoft.com/en-us/library/ms228362.aspx .
Upvotes: 0
Reputation:
Yes, just change the \
to a \\
.
You can read more about Escape Sequences here.
Upvotes: 3