297

How can I escape double quotes in a string?

I tried:

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

This worked -- "" becomes ".

Is there a C# function or other method to escape double quotes so that no changing in string is required?

4

8 Answers 8

357

Yes, you can escape the " using a backslash:

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

Otherwise you have to use verbatim string literals as you have.

The string has not changed in either case - there is a single escaped " in it. This is just a way to tell C# that the character is part of the string and not a string terminator.

2
  • why when using @ does not work using \?
    – toha
    Commented Sep 16, 2022 at 5:52
  • 2
    @toha - because that's how they were defined. A verbatim string literal (@ string) treats a backslash `\` as nothing more than that. It isn't treated as an escape sequence. It is kind of the point of verbatim string literals.
    – Oded
    Commented Sep 16, 2022 at 10:28
155

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 the same that is printed with:

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

Here is a DEMO.

" is still part of your string.

You can check Jon Skeet's Strings in C# and .NET article for more information.

0
27

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 other characters

  \0 nul 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

Any character substitution by numbers:

  \xh to \xhhhh, or \uhhhh - Unicode character in hexadecimal notation (\x has variable digits, \u has 4 digits)
  \Uhhhhhhhh - Unicode surrogate pair (8 hex digits, 2 characters)
20

Another thing worth mentioning from C# 6 is interpolated strings can be used along with @.

Example:

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

Or

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

Check running code here!

View the reference to interpolation here!

2
  • From what C# version is this valid for? Can you provide some references? Please respond by editing your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Commented Jun 19, 2021 at 16:42
  • 2
    Note that bouth $@"" and @$"" are valids. Commented Mar 7, 2022 at 15:19
17

C#11 introduces a new feature called "raw string literals." To quote the Microsoft documentation:

Beginning with C# 11, you can use raw string literals to more easily create strings that are multi-line, or use any characters requiring escape sequences. Raw string literals remove the need to ever use escape sequences. You can write the string, including whitespace formatting, how you want it to appear in output."

SOURCE: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#raw-string-literals

EXAMPLE: So using the original example, you could do this (note that raw string literals always begin with three or more quotation marks):

string testSingleLine = """He said to me, "Hello World". How are you?""";
string testMultiLine = """
He said to me, "Hello World". How are you?
""";
0
8

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.

6

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 == 1

"\"".Length == 1
0

In C#, there are at least four 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.

2
  • 1
    It would be better if you summarised here (by editing your answer) - as the link may break at any time. E.g., what exactly is meant by "Use the corresponding ASCII character"? How exactly is it encoded in source code? You could provide one or more (awesome) code examples for each of the four ways. Commented Jun 19, 2021 at 16:37
  • The official Microsoft doc. Your (3.) is not mentioned. Perhaps you mean \xHH, which is one usage of the variable-length \xH... form. Note: ASCII is the first 128 characters of Unicode. (4.) has two forms: \uHHHH for 16-bit Unicode characters. U00HHHHHH for 32-bit Unicode characters (which become TWO .Net char characters in the string). Commented 11 hours ago

Not the answer you're looking for? Browse other questions tagged or ask your own question.