C# 如何在字符串文字中引用 \"(斜线双引号)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9688159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:26:08  来源:igfitidea点击:

How to quote \" (slash double-quote) in a string literal?

c#stringliterals

提问by Bali C

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 \u0022but don't want to unless I can help it.

这可能是一个非常简单的问题,但我似乎无法理解它。我需要一个字符串,它包含\"但不将其视为转义字符。我尝试使用@但它不起作用。我想这样做的唯一另一种方法是使用,\u0022但除非我能帮上忙,否则我不想这样做。

Desired string - string s = "\"\""; // Obviously this doesn't work!

所需的字符串 - string s = "\"\""; // Obviously this doesn't work!

Desired console output - \"\"

所需的控制台输出 - \"\"

Hope this makes sense.

希望这是有道理的。

Thanks!

谢谢!

采纳答案by Michael Rice

Try

尝试

string s = "\\"\\"";

You have to escape your backslashes too.

你也必须逃避你的反斜杠。

Mike

麦克风

回答by Kirill Polishchuk

Use this string:

使用这个字符串:

string s = "\\"\\"";

回答by Mongus Pong

Console.WriteLine( "\\"\\"" );

Just put a \before each character that needs to be printed.

只需\在需要打印的每个字符之前放置一个。

回答by Colleen

I think you have to escape backslashes too... so something like "\\\"\\\""should work, I believe.

我认为你也必须逃避反斜杠......所以像这样的东西"\\\"\\\""应该可以工作,我相信。

回答by Colleen

In verbatim string literals (@"...") a "in the string value is encoded as "", which happens to also be the onlyescape sequence in verbatim strings.

在逐字字符串文字 ( @"...")"中,字符串值中的 a 被编码为"",这恰好也是逐字字符串中唯一的转义序列。

@"\""Happy coding!\"""     // => \"Happy coding!\"

"\\"Happy coding!\\""    // => \"Happy coding!\"

Note that in the 2nd case (not a verbatim string literal), a \is required before the \andthe "to escape them and prevent their normal meanings.

请注意,在第二种情况下(不是逐字字符串文字), a\需要在 the\the之前对"它们进行转义并防止它们的正常含义。

See the C# string referencefor more details and examples.

有关更多详细信息和示例,请参阅C# 字符串参考

回答by Sam Axe

String s = @"\""\""";

DblQuote characters will escape a second dblquote character

DblQuote 字符将转义第二个 dblquote 字符

Though for better readability I would go with:

虽然为了更好的可读性,我会选择:

const String DOUBLEQUOTE = """";
const String BACKSLASH = @"\";

String s = BACKSLASH + DOUBLEQUITE + BACKSLASH + DOUBLEQUOTE;

回答by Eric Andres

In a verbatim string (a string starting with @"") to escape double quotes you use double quotes, e.g. @"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).

在逐字字符串(以 开头的字符串@"")中,您可以使用双引号来转义双引号,例如@"Please press ""Ok"".". 如果你想用逐字字符串来做,那么你会做类似的事情@"\"""(最后有 3 个双引号)。

回答by Lynn Crumbling

You can use the literal, but you need to double-up quotes.

您可以使用文字,但需要双引号。

   string s = @"\""\"""; 

回答by Smit Patel

You can do like this,

你可以这样做,

string s = "something'\\'";

Use a single '' rather then "" in string to do the same.

在字符串中使用单个 '' 而不是 "" 来做同样的事情。