This is probably a really simple question but I can't seem to get my head around it. I need to have a string that contains \\"
without it seeing it as an escape character. I tried using @
but it won't work. The only other way I thought of doing this would be to use "
but don't want to unless I can help it.
Desired string - string s = "\\"\\""; // Obviously this doesn't work!
string s = "\\"\\""; // Obviously this doesn't work!
Desired console output - \\"\\"
Hope this makes sense.
Thanks!
Try
string s = "\\\"\\\"";
You have to escape your backslashes too.
Mike
In verbatim string literals ( @"..."
) a "
in the string value is encoded as ""
, which happens to also be the only escape sequence in verbatim strings.
@"\""Happy coding!\""" // => \"Happy coding!\"
"\\\"Happy coding!\\\"" // => \"Happy coding!\"
Note that in the 2nd case (not a verbatim string literal), a \\
is required before the \\
and the "
to escape them and prevent their normal meanings.
See the C# string reference for more details and examples.
您可以使用文字,但需要加倍引号。
string s = @"\""\""";
我认为你也必须逃避反斜杠...所以像"\\\\\\"\\\\\\""
这样的东西应该有效,我相信。
使用此字符串:
string s = "\\\"\\\"";
Console.WriteLine( "\\\"\\\"" );
只需在需要打印的每个字符前放一个\\
。
String s = @"\""\""";
DblQuote characters will escape a second dblquote character
Though for better readability I would go with:
const String DOUBLEQUOTE = """";
const String BACKSLASH = @"\";
String s = BACKSLASH + DOUBLEQUITE + BACKSLASH + DOUBLEQUOTE;
In a verbatim string (a string starting with @""
) to escape double quotes you use double quotes, eg @"Please press ""Ok""."
. If you want to do it with verbatim strings then you would do something like @"\\"""
(that's 3 double quotes on the end there).
You can do like this,
string s = "something'\\\'";
Use a single '' rather then "" in string to do the same.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.