Jackson Siro Jackson Siro - 9 months ago 40
Java Question

Replacing " Character by another character in a string

How do I replace Double quotes a well as single quotes for instance:
I want where there is a " to be replaced by nothing at all. I have tried

String quoteid3 = quoteid2.replace('"','');

and it causes an error.

Answer Source

If you want to replace " , escape the the double quote \", Similarly escape ' with \'

Use replaceAll() to provide a regex [\"\'] that recognizes all ' and " , and replace it with "" from a String to remove the values (replace them with nothing).

String quoteid3 = quoteid2.replaceAll("[\"\']", "");

Or alternatively if you want to stick to using replace() without regex:-

String quoteid3 =quoteid2.replace("\"","").replace("\'","");