28

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.

Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
jimmy
  • 7,715
  • 11
  • 32
  • 39

4 Answers4

95
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"""
gprathour
  • 14,313
  • 5
  • 60
  • 89
switz
  • 22,934
  • 23
  • 74
  • 100
9

You need to escape the quotation marks:

String name = "\"john\"";
Anthony
  • 11,737
  • 9
  • 66
  • 101
6

You can add escaped double quotes like this: String name = "\"john\"";

Thomas
  • 84,542
  • 12
  • 116
  • 151
3

You can do this using Escape Sequence.

\"

So you will have to write something like this :

String name = "\"john\"";

You can learn about Escape Sequences from here.

gprathour
  • 14,313
  • 5
  • 60
  • 89