-2

I'm trying to replace \" backslash and quote to ", those characters are together in my text, so I do a replace but the compiler it say that my escaping syntax is incorrect:

String myText = "Animal:{\"name\":\"turkey\"}";

As you can observe here there is \" together so I'm want to replace by just one quote " To look like this:

"Animal:{"name":"turkey"}"

So my replace its looks like:

myText.replaceAll("\\\"", "\"");
  • To escape a backslash we need \\
  • To escape a quote we need \"

With that logic I have this \\\" but its not working, its incorrect for the compiler... I already tried:

myText.replaceAll("\\*", "");

But this one is not want I want in my string.

Any advice?

  • What do you mean by "its incorrect for the compiler"? – Andrew Vershinin Jul 22 '20 at 19:39
  • What is the error? – Unmitigated Jul 22 '20 at 19:39
  • Your 1st line of syntax is not Java. Is that a typo? String myText: "Animal:{\"name\":\"turkey\"}" – tune5ths Jul 22 '20 at 19:41
  • I think the compiler error is that you are breaking up a perfectly good String into a String mixed with 2 variables `"Animal:{"name":"turkey"}"`. See that you all of a sudden introduced the variable _name_ and _turkey_. Maybe share the compiler error, although I suspect it will point to this problem anyway. – Fullslack Jul 22 '20 at 19:45
  • Can you tell us where that String come from? I feel like you're receiving a Json response from an API and you want to escape the quotes manually instead of de-serializing / serializing the message correctly. using a library such as Jackson.. – Matteo NNZ Jul 22 '20 at 20:23

2 Answers2

1

I think you are exposing the problem wrongly. If you write a String like this in the source code:

String myText = "Animal:{\"name\":\"turkey\"}";

... the String is already escaped. It means you may print :

System.out.println(myText);

... and you would get in output:

Animal:{"name":"turkey"}

... without need of doing anything else.

So I can only imagine two things:

  1. Your question is: Can I write String myText = "Animal:{"name":"turkey"}"; without backslashes in the source code? => The answer is no, or the compiler wouldn't know what's the delimiter of the text and what's just another character of the text.

  2. Your question is missing information: for example, you are receiving this String from a service which is responding in Json, and over the transport this String is keeping the backslashes on the quotes.

If that's the case, you should rather use the proper library to parse the message as JsonNode. Add a dependency to jackson-databind into your project and then use these methods:

ObjectMapper mapper = new ObjectMapper(); // <-- create ObjectMapper
JsonNode actualObj = mapper.readTree(jsonString); // <-- parse your Json string into a JsonNode
String niceJsonString = actualObj.toPrettyString(); // <-- formats the Json properly

If your question falls in my second guess, then I suggest you have a look at Jackson, it is a pretty powerful library to work with Json (a market standard for Json messaging in Java).

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
0

The problem is that the first argument of replaceAll is not just a String, it is a regex string, and regex also uses backslash to escape the next character. So you need another entire level of escaping, so that what you are passing to regex is the string you have, i.e. escape backslash escape quote, so that the regex will search for the sequence backslash quote.

myText.replaceAll("\\\\\\\"", "\"");

I think that's right.

Zag
  • 638
  • 4
  • 8