-1

Tried to store "\"the Dolphin\"" using this but it isn't working.

String s=""\"the Dolphin\""";
Tanmay Awasthi
  • 69
  • 1
  • 2
  • 8

2 Answers2

1

Try this

String s="\"\\\"the Dolphin\\\"\"";

you have to escape special chars via \

Milkmaid
  • 1,659
  • 4
  • 26
  • 39
0

" and \ are special characters in String literal

  • " is used to mark start or end of string
  • \ is used to either
    • create special characters like tabulator \t or line breaks \n \r (and few others)
    • escape other metacharacters to make them simple literals like \" will let you use " inside string (it will not represent end of string literal now so string like "hello\"world" is valid)

So if you want to create literals from special characters you need to escape them with \. Same rule applies to \ itself so if you want to create string which will represent \ you need to write it as "\\" (\ is escaped with another \)

So try with "\"\\\"the Dolphin\\\"\""

String s = "\"\\\"the Dolphin\\\"\"";
//           ↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓ ↓
//literals:  " \ "the Dolphin \ " "
//finally:     "\"the Dolphin\""
Pshemo
  • 122,468
  • 25
  • 185
  • 269