C# 我可以在逐字字符串文字中转义双引号吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1928909/
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
Can I escape a double quote in a verbatim string literal?
提问by kdt
In a verbatim string literal (@"foo") in C#, backslashes aren't treated as escapes, so doing \" to get a double quote doesn't work. Is there any way to get a double quote in a verbatim string literal?
在 C# 中的逐字字符串文字 (@"foo") 中,反斜杠不被视为转义符,因此执行 \" 来获取双引号不起作用。有没有办法在逐字字符串文字中获取双引号?
This understandably doesn't work:
这可以理解是行不通的:
string foo = @"this \"word\" is escaped";
采纳答案by Myles
Use a duplicated double quote.
使用重复的双引号。
@"this ""word"" is escaped";
outputs:
输出:
this "word" is escaped
回答by Brandon
Use double quotation marks.
使用双引号。
string foo = @"this ""word"" is escaped";
回答by rfonn
This should help clear up any questions you may have: c# literals
这应该有助于澄清您可能遇到的任何问题:c#literals
Here is a table from the linked content:
这是链接内容中的表格:
回答by j.a.estevan
For adding some more information, your example will work without the @
symbol (it prevents escaping with \), this way:
为了添加更多信息,您的示例将在没有@
符号的情况下工作(它可以防止用 \ 转义),这样:
string foo = "this \"word\" is escaped!";
It will work both ways but I prefer the double-quote style for it to be easier working, for example, with filenames (with lots of \ in the string).
它可以双向工作,但我更喜欢双引号样式,因为它更容易工作,例如,使用文件名(字符串中有很多 \)。