Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I escape a double quote in a verbatim string literal?

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"; 
like image 547
kdt Avatar asked Dec 18 '09 15:12

kdt


People also ask

How do you escape a double quote in 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' .

Are string literals enclosed in double quotes?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

How do you escape a string literal?

String literal syntaxUse the escape sequence \n to represent a new-line character as part of the string. Use the escape sequence \\ to represent a backslash character as part of the string. You can represent a single quotation mark symbol either by itself or with the escape sequence \' .

What is verbatim string literal?

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello".


2 Answers

Use a duplicated double quote.

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

outputs:

this "word" is escaped 
like image 164
Myles Avatar answered Sep 22 '22 22:09

Myles


Use double quotation marks.

string foo = @"this ""word"" is escaped"; 
like image 32
Brandon Avatar answered Sep 23 '22 22:09

Brandon