Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape double quotes in a string

People also ask

How do you escape a double quote in a string?

Double Quotes inside verbatim strings can be escaped by using 2 sequential double quotes "" to represent one double quote " in the resulting string. var str = @"""I don't think so,"" he said. "; Console. WriteLine(str);

How do you escape a quote from a string?

To escape a single or double quote in a string, use a backslash \ character before each single or double quote in the contents of the string, e.g. 'that\'s it' .

How do you escape a double quote in a string in java?

Double quotes characters can be escaped with backslash( \ ) in java.

How do you write a double quote in a string?

A double-quoted string can have single quotes without escaping them, conversely, a single-quoted string can have double quotes within it without having to escape them. Double quotes ( \" ) must escape a double quote and vice versa single quotes ( \' ) must escape a single quote.


No.

Either use verbatim string literals as you have, or escape the " using backslash.

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

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.


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.


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)

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.


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