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

前端 未结 4 2092
不知归路
不知归路 2020-11-22 01:11

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 qu

相关标签:
4条回答
  • 2020-11-22 01:26

    Use a duplicated double quote.

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

    outputs:

    this "word" is escaped
    
    0 讨论(0)
  • 2020-11-22 01:38

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

    0 讨论(0)
  • 2020-11-22 01:50

    Use double quotation marks.

    string foo = @"this ""word"" is escaped";
    
    0 讨论(0)
  • 2020-11-22 01:51

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

    Here is a table from the linked content:

    0 讨论(0)
提交回复
热议问题