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
  • 795,719
  • 175
  • 1,089
  • 1,143
jimmy
  • 8,121
  • 11
  • 36
  • 40

4 Answers4

99
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,813
  • 5
  • 66
  • 90
switz
  • 24,384
  • 25
  • 76
  • 101
10

You need to escape the quotation marks:

String name = "\"john\"";
Anthony
  • 12,177
  • 9
  • 69
  • 105
6

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

Thomas
  • 87,414
  • 12
  • 119
  • 157
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,813
  • 5
  • 66
  • 90