Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quote \" (slash double-quote) in a string literal?

This is probably a really simple question but I can't seem to get my head around it. I need to have a string that contains \" without it seeing it as an escape character. I tried using @ but it won't work. The only other way I thought of doing this would be to use \u0022 but don't want to unless I can help it.

Desired string - string s = "\"\""; // Obviously this doesn't work!

Desired console output - \"\"

Hope this makes sense.

Thanks!

like image 809
Bali C Avatar asked Mar 13 '12 16:03

Bali C


People also ask

How do you write a double quote as a string literal?

To represent a double quotation mark in a string literal, use the escape sequence \". The single quotation mark (') can be represented without an escape sequence. The backslash (\) must be followed with a second backslash (\\) when it appears within a string.

How do you insert a double quote into a quoted string?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.

How do you split a string with double quotes?

split("(? =\"[^\"]. *\")");

How do you escape quotation marks in a string?

You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example. The backslashes protect the quotes, but are not printed.


1 Answers

You can use the literal, but you need to double-up quotes.

   string s = @"\""\"""; 
like image 147
Lynn Crumbling Avatar answered Sep 23 '22 08:09

Lynn Crumbling