Replace all double quotes within String

I am retrieving data from a database, where the field contains a String with HTML data. I want to replace all of the double quotes such that it can be used for parseJSON of jQuery.

Using Java, I am trying to replace the quotes using..

details.replaceAll("\"","\\\"");
  //details.replaceAll("\"","&quote;"); details.replaceAll("\"","&#34");

The resultant string doesn't show the desired change. An O'Reilly article prescribes using Apache string utils. Is there any other way??

Is there a regex or something that I could use?


Solution 1:

Here's how

String details = "Hello \"world\"!";
details = details.replace("\"","\\\"");
System.out.println(details);               // Hello \"world\"!

Note that strings are immutable, thus it is not sufficient to simply do details.replace("\"","\\\""). You must reassign the variable details to the resulting string.


Using

details = details.replaceAll("\"","&quote;");

instead, results in

Hello &quote;world&quote;!

Solution 2:

Would not that have to be:

.replaceAll("\"","\\\\\"")

FIVE backslashes in the replacement String.