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("\"",""e;"); details.replaceAll("\"",""");
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?
Use the String. replaceAll() method to replace single with double quotes, e.g. const replaced = str. replaceAll("'", '"'); . The replaceAll method will return a new string where all occurrences of single quotes are replaced with double quotes.
Add double quotes to String in java If you want to add double quotes(") to String, then you can use String's replace() method to replace double quote(") with double quote preceded by backslash(\").
Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.
So here in our first statement, we first generate a string with double quotes. Then we call the rstrip() function and pass ('\')as a parameter to remove double-quotes. Then we use two print functions. The first one displays the original string and the second one displays the new filtered string.
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;!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With