Can I escape a double quote in a verbatim string literal?
Asked Answered
B

7

584

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";
Bruch answered 18/12, 2009 at 15:36 Comment(1)
The most comprehensive answer is rfonn's answer (e.g., for folks ending up here from a search engine, e.g. searching for "escape quotes C#")Syzygy
B
927

Use a duplicated double quote.

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

outputs:

this "word" is escaped
Brackett answered 18/12, 2009 at 15:37 Comment(4)
I'm trying to use @Html.Raw() and the quadruple quote breaks my stringBeitnes
Can you use it without the string literal? It seems like Html.Raw() would take care of ensuring it's a string for you...Brackett
It's also possible to use it in combination with string interpolation: $@"this ""{wordVar}"" is escaped";.Chambermaid
Perhaps be explicit about whether this is the only way or not? What about later developments, like C# 6? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)Syzygy
H
119

Use double quotation marks.

string foo = @"this ""word"" is escaped";
Hesiod answered 18/12, 2009 at 15:37 Comment(0)
Z
96

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

Here is a table from the linked content:

Regular literal Verbatim literal Resulting string
"Hello" @"Hello" Hello
"Backslash: \\" @"Backslash: \" Backslash: \
"Quote: \"" @"Quote: """ Quote: "
"CRLF:\r\nPost CRLF" @"CRLF:
Post CRLF"
CRLF:
Post CRLF
Zaibatsu answered 18/12, 2009 at 15:42 Comment(4)
What about escaping of { and }?Syzygy
@PeterMortensen - Neither of those syntaxes require any escape for { or }. Are you thinking of interpolated string syntax $?Souvaine
A lot was added to C# since its first inception. Do we have a way define string literals with lots of double quotes without having to escape them nowadays? (i.e. by using another string delimiter like we can use in sed or perl?)Progressist
user1234567's answer covers string brace escape sequences, {{ and }}.Syzygy
C
96

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

Caz answered 18/12, 2009 at 15:51 Comment(0)
M
8

Update: With C# 11 Preview feature - Raw String Literals

string foo1 = """
   this "word" is escaped
   """;

string foo2 = """this "word" is escaped""";

History:

There is a proposal open in GitHub for the C# language about having better support for raw string literals. One valid answer, is to encourage the C# team to add a new feature to the language (such as triple quote - like Python).

see https://github.com/dotnet/csharplang/discussions/89#discussioncomment-257343

Martie answered 4/1, 2021 at 0:12 Comment(2)
This got upgraded to a real proposal github.com/dotnet/csharplang/issues/4304, and now it's in the C#11 Preview! see learn.microsoft.com/en-us/dotnet/csharp/whats-new/…Martie
Exactly what I need in LINQPad (so dotnet7 preview it is...)Progressist
K
2

As the documentation says:

Simple escape sequences ... are interpreted literally. Only a quote escape sequence ("") is not interpreted literally; it produces one double quotation mark. Additionally, in case of a verbatim interpolated string brace escape sequences ({{ and }}) are not interpreted literally; they produce single brace characters.

Kabyle answered 18/3, 2021 at 9:16 Comment(1)
NOTE: only interpolated string syntax $"..." requires escaping of { and }. Regular strings "..." and verbatim strings @"..." do not require those to be escaped.Souvaine
G
0

Since C#11 you can use raw strings, which are delimited by three double quotes.

They can be either single or multiline:

var singleLine = """Anything, including " is supported here without escaping""";
var multiLine = """
    Any blank after the opening quotes in previous line is discarded.
    This is WYSIWYG, including "quotes" and new lines.
        This line is indented by 4 characters, not 8, because
    the number of characters before the closing quotes
    of the next line are removed from each line.
    """;

The multiline version keeps the new lines and indenting, but interestingly it discards the characters before the indentation of the closing """ as explained above. And, as expected, the first line is the one below the opening, and the last is the one before the closing quotes.

And you can also use @""" for interpolation, like

var @"""
    The value of "{property}" is:
    {value}
    """;

NOTE: I formatted the code as python in this answer, because the C# formatter is not aware of this yet, and python formatter keeps this more readable

Ginseng answered 19/11, 2023 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.