Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quotation marks inside a string [duplicate]

Tags:

java

string

I need some help from you guys. I have a string name = "john"

But I want to save this String name as "john", including ""(quotations)

String name = ""john""; String name1 = "[john]" 

Can some one help me with this.

like image 557
jimmy Avatar asked Dec 02 '11 09:12

jimmy


People also ask

How can you include a double quote inside a double 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 remove double quotes from a replacement string?

Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.

How do you use quotation marks in a string?

Within a character string, to represent a single quotation mark or apostrophe, use two single quotation marks. (In other words, a single quotation mark is the escape character for a single quotation mark.) A double quotation mark does not need an escape character.


2 Answers

String name = "\"john\""; 

You have to escape the second pair of quotation marks using the \ character in front. It might be worth looking at this link, which explains in some detail.

Other scenario where you set variable:

String name2 = "\""+name+"\""; 

Sequence in console:

> String name = "\"john\""; > name ""john"" > String name2 = "\""+name+"\""; > name2 """john""" 
like image 169
switz Avatar answered Oct 17 '22 22:10

switz


You need to escape the quotation marks:

String name = "\"john\""; 
like image 39
Anthony Avatar answered Oct 17 '22 22:10

Anthony