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";
开发者_如何学Go
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.
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";
Normally, this should work. An alternative is:
string str = @"abc\xyz";
public class Program
{
static void Main(string[] args)
{
string str = "abc\\xyz";
Console.WriteLine(str);
}
}
This works fine. It prints abc\xyz
.
You can do it like this
string str = @"abc\xyz";
string str = @"abc\xyz";
You can do this:
string str = @"abc\xyz";
That tells says that the slash is significant and not an escape character.
You'll need to prefix the string with the '@' symbol to stop the compiler trying to treat it as an escape sequence. So string str = @"abc\xyz";
should work.
精彩评论