583

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";
abatishchev
  • 98,240
  • 88
  • 296
  • 433
kdt
  • 27,905
  • 33
  • 92
  • 139
  • The most comprehensive answer is *[rfonn's answer](https://stackoverflow.com/questions/1928909/can-i-escape-a-double-quote-in-a-verbatim-string-literal/1928943#1928943)* (e.g., for folks ending up here from a search engine, e.g. searching for "escape quotes C#") – Peter Mortensen Oct 07 '21 at 15:36

6 Answers6

925

Use a duplicated double quote.

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

outputs:

this "word" is escaped
Palec
  • 12,743
  • 8
  • 69
  • 138
Myles
  • 20,860
  • 4
  • 28
  • 37
  • I'm trying to use @Html.Raw() and the quadruple quote breaks my string – JoshYates1980 Sep 26 '14 at 20:35
  • 1
    Can you use it without the string literal? It seems like Html.Raw() would take care of ensuring it's a string for you... – Myles Sep 27 '14 at 17:32
  • 3
    It's also possible to use it in combination with string interpolation: `$@"this ""{wordVar}"" is escaped";`. – fdelia Jan 30 '18 at 10:02
  • 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.) – Peter Mortensen Aug 14 '21 at 21:18
119

Use double quotation marks.

string foo = @"this ""word"" is escaped";
Brandon
  • 68,708
  • 30
  • 194
  • 223
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).

Mark Mayo
  • 12,230
  • 12
  • 54
  • 85
j.a.estevan
  • 3,057
  • 18
  • 32
95

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
Nick
  • 138,499
  • 22
  • 57
  • 95
rfonn
  • 2,161
  • 12
  • 8
  • What about escaping of `{` and `}`? – Peter Mortensen Oct 07 '21 at 15:33
  • @PeterMortensen - Neither of those syntaxes require any escape for `{` or `}`. Are you thinking of `interpolated string` syntax `$`? – ToolmakerSteve Jul 10 '22 at 21:28
  • 1
    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?) – mbx Aug 12 '22 at 21:46
  • [user1234567's answer](https://stackoverflow.com/questions/1928909/can-i-escape-a-double-quote-in-a-verbatim-string-literal/66688029#66688029) covers string brace escape sequences, {{ and }}. – Peter Mortensen Sep 21 '22 at 14:21
6

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

Kind Contributor
  • 17,547
  • 6
  • 53
  • 70
  • This got upgraded to a real proposal https://github.com/dotnet/csharplang/issues/4304, and now it's in the C#11 Preview! see https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals – Kind Contributor May 15 '22 at 14:29
  • Exactly what I need in LINQPad (so dotnet7 preview it is...) – mbx Aug 12 '22 at 21:49
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1234567
  • 3,991
  • 3
  • 19
  • 25
  • 1
    NOTE: **only** `interpolated string` syntax `$"..."` requires escaping of `{` and `}`. Regular strings `"..."` and verbatim strings `@"..."` do not require those to be escaped. – ToolmakerSteve Jul 10 '22 at 21:38