-1

I want to replace " in my string to \\" for later use by Javascript JSON.parse(...), I try the following test

String name = "\"ab\"c";
System.out.println("name before escape="+name);
String name1 = name.replaceAll("\"", "\\\"");
System.out.println("name1="+name1);
String name2 = name.replaceAll("\"", "\\\\\"");
System.out.println("name2="+name2);
String name3 = name.replaceAll("\"", "\\\\\\\"");
System.out.println("name3="+name3);

and result as follow:

name before escape="ab"c
name1="ab"c
name2=\"ab\"c
name3=\"ab\"c

so all fail, and I don't understand the output result

  1. why name2 and name3 are the same?
  2. how to replace all " to \\"

[Update1}

for question 2, I found the following work

System.out.println("name4=" + name.replaceAll("\"", Matcher.quoteReplacement("\\\\\"")));

Although I feel lost for the reason it works...

user1169587
  • 1,104
  • 2
  • 17
  • 34
  • Don't construct your own JSON. Use a library. We get plenty of JavaScript questions here on Stack Overflow that ask how to fix malformed JSON (which is often near to impossible) because the source they got it from didn't encode it correctly. Don't make that mistake. – Ivar Feb 19 '21 at 11:09
  • Also, what is your exact desired output? – Olivier Grégoire Feb 19 '21 at 11:12
  • @ivar any library recommended? – user1169587 Feb 19 '21 at 17:25
  • @OlivierGrégoire my desired output is \\"ab\\"c – user1169587 Feb 19 '21 at 17:26
  • You can use Jackson, Gson or JSON-java. If you only want to encode one string, then you can use it [as shown here](https://stackoverflow.com/questions/21576475/escape-json-string-in-java/40430760). But if it is part of a bigger JSON, then you can also [convert a complete POJO into JSON](https://www.baeldung.com/jackson-object-mapper-tutorial). – Ivar Feb 19 '21 at 17:38

3 Answers3

2

It's better to use replace() instead of replaceAll():

String name = "\"ab\"c";
System.out.println("name before escape=" + name);
System.out.println("name1=" + name.replace("\"", "\\\\\""));

Output:

name before escape="ab"c
name1=\\"ab\\"c
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • but if I want to use replaceAll, then how to write? and how to interpret the result of name1, name2 and name3 in my question? – user1169587 Feb 19 '21 at 17:23
1

Don't use replaceAll, use replace: name.replace("\"", "\\\""); The reason is that replaceAll uses regex and it can mess all your formatting up.

class Main {
  public static void main(String[] args) {
    String name = "\"ab\"c";
    String name1 = name.replace("\"", "\\\\\"");
    System.out.println(name1); // Prints: \\"ab\\"c
  }
}
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
0

use replaceAll need to escape \ and "

String name = "\"ab\"c";
System.out.println("name before escape="+name);
String name1 = name.replaceAll("\\\"", "\\\\\\\\\"");
System.out.println("name1="+name1);
icejoywoo
  • 151
  • 5
  • No, this output name1=\"ab\"c, NOT \\"ab\\"c – user1169587 Feb 19 '21 at 17:27
  • 1
    just update the answer, name.replaceAll("\\\"", "\\\\\\\\\"") – icejoywoo Feb 20 '21 at 03:44
  • but can explain why this work? there is 9 backslash, the first three \\\, as \ has special meaning in java and regex, so one backslash need add two more backslash to esapce, so from left to right, the first six backslash output \\, but how to explain the remaining 7th, 8th, 9th backslash plus the double quote \\\" ? – user1169587 Feb 20 '21 at 06:10
  • 2 escape in string and regex string. string defination "\\\\\\\\\"", \\ -> \, \" -> ", so raw string is\\\\". in regex string also need escape, so \\ -> \, \" -> ", so it's \\" – icejoywoo Feb 20 '21 at 07:24
  • first is java escape, so 1st, 2nd backslash means one backslash, 3rd, 4th the second, 5th, 6th the third, 7th, 8th the fourth, remaining \", means ", so the raw string is four backslash plus one double quotes \\\\" then in regex expression, first backslash and second backslash output one backslash, 3rd, 4th backslash output the second backslash, so becomses \\" Do you mean this? – user1169587 Feb 20 '21 at 07:48
  • yep, that's what I mean – icejoywoo Feb 20 '21 at 08:00
  • Ask for more, Java escape is always interpret first before regex escape? – user1169587 Feb 20 '21 at 08:25
  • yes, this is firstly a java string literal, so java escape is always interpreted first – icejoywoo Feb 20 '21 at 11:11