[c#] Escape double quotes in a string

Double quotes can be escaped like this:

string test = @"He said to me, ""Hello World"". How are you?";

But this involves adding character " to the string. Is there a C# function or other method to escape double quotes so that no changing in string is required?

This question is related to c# string double-quotes

The answer is


Please explain your problem. You say:

But this involves adding character " to the string.

What problem is that? You can't type string foo = "Foo"bar"";, because that'll invoke a compile error. As for the adding part, in string size terms that is not true:

@"""".Length == "\"".Length == 1

In C# you can use the backslash to put special characters to your string. For example, to put ", you need to write \". There are a lot of characters that you write using the backslash: Backslash with a number:

  • \000 null
  • \010 backspace
  • \011 horizontal tab
  • \012 new line
  • \015 carriage return
  • \032 substitute
  • \042 double quote
  • \047 single quote
  • \134 backslash
  • \140 grave accent

Backslash with othe character

  • \a Bell (alert)
  • \b Backspace
  • \f Formfeed
  • \n New line
  • \r Carriage return
  • \t Horizontal tab
  • \v Vertical tab
  • \' Single quotation mark
  • \" Double quotation mark
  • \ Backslash
  • \? Literal question mark
  • \ ooo ASCII character in octal notation
  • \x hh ASCII character in hexadecimal notation
  • \x hhhh Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal. For example, WCHAR f = L'\x4e00' or WCHAR b[] = L"The Chinese character for one is \x4e00".

You're misunderstanding escaping.

The extra " characters are part of the string literal; they are interpreted by the compiler as a single ".

The actual value of your string is still He said to me , "Hello World".How are you ?, as you'll see if you print it at runtime.


In C#, there are at least 4 ways to embed a quote within a string:

  1. Escape quote with a backslash
  2. Precede string with @ and use double quotes
  3. Use the corresponding ASCII character
  4. Use the Hexadecimal Unicode character

Please refer this document for detailed explanation.


You can use backslash either way;

string str = "He said to me, \"Hello World\". How are you?";

It prints;

He said to me, "Hello World". How are you?

which is exactly same prints with;

string str = @"He said to me, ""Hello World"". How are you?";

Here is a DEMO.

" is still part of your string.

Check out Escape Sequences and String literals from MSDN.


One solution, is to add support to the csharp language so that "" isn't the only scheme used for strings.

For another string terminator to the C# language - I'm a fan of backtick in ES6.

string test = `He said to me, "Hello World". How are you?`;

But also, the doubling idea in Markdown might be better:

string test = ""He said to me, "Hello World". How are you?"";

The code does not work at the date of this post. This post is a solution where the visitors to this Q&A jump onto this csharplank ticket for C# and upvote it - https://github.com/dotnet/csharplang/discussions/3917