Jackson Siro Jackson Siro - 20 days ago 7
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

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("\'","");
Comments