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 parseJS
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("\"",""e;");
instead, results in
Hello "e;world"e;!
String info = "Hello \"world\"!";
info = info.replace("\"", "\\\"");
String info1 = "Hello "world!";
info1 = info1.replace('"', '\"').replace("\"", "\\\"");
For the 2nd field info1, 1st replace double quotes with an escape character.