Can I escape a double quote in a verbatim string literal?

旧巷老猫 提交于 2019-11-25 22:28:29

问题


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?

This understandably doesn\'t work:

string foo = @\"this \\\"word\\\" is escaped\";

回答1:


Use a duplicated double quote.

@"this ""word"" is escaped";

outputs:

this "word" is escaped



回答2:


Use double quotation marks.

string foo = @"this ""word"" is escaped";



回答3:


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).




回答4:


This should help clear up any questions you may have: c# literals

Here is a table from the linked content:



来源:https://stackoverflow.com/questions/1928909/can-i-escape-a-double-quote-in-a-verbatim-string-literal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!