Can I escape a double quote in a verbatim string l

2018-12-31 05:42发布

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";

4条回答
不流泪的眼
2楼-- · 2018-12-31 05:52

Use a duplicated double quote.

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

outputs:

this "word" is escaped
查看更多
弹指情弦暗扣
3楼-- · 2018-12-31 05:53

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

Here is a table from the linked content:

enter image description here

查看更多
十年一品温如言
4楼-- · 2018-12-31 06:02

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

查看更多
姐姐魅力值爆表
5楼-- · 2018-12-31 06:05

Use double quotation marks.

string foo = @"this ""word"" is escaped";
查看更多
登录 后发表回答