I want to use \ in a string, like
string str="abc\xyz";
But this is giving me error.
I have also tried
string str="abc\\xyz";
But still it isnt working. Can anyone help me out?
You can either escape the character, like so:
string str="abc\\xyz";
or use a verbatim string literal like so:
string str=@"abc\xyz";
So your second example should work.
See here for more information.
verbatim string literal.Well, the latter ("abc\\xyz") will certainly result in a backslash in the string - or you could use a verbatim string literal:
string str = @"abc\xyz";
Note that if you use the debugger to look at your strings, it will often (always?) "escape" them for you, so you'd see "abc\\xyz". This can cause a fair amount of confusion. Either look at the characters individually, or print the string to the console.
You haven't said in what way it "isn't working" - could you give more details? If it's just the debugger output, then the above may be all you're looking for - but otherwise you should tell us what you're seeing vs what you expected to see.
Have a look at my article about strings for more information about strings in general, escaping, the debugger etc.
string str = "abc\\xyz"- i.e. just two back slashes? Having three gives "unrecognised escape sequence"