Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace a single quote in a string with another single quote

Tags:

java

I have a String with single quote. I want to replace the single quote with 2 single quotes. I tried using

 String s="Kathleen D'Souza";

s.replaceAll("'","''");

s.replaceAll("\'","\'\'");

s.replace("'","''");

s.replace("\'","\'\'");

But the single quote is not getting replaced with 2 single quotes.

like image 544
Vidya Avatar asked Feb 22 '13 05:02

Vidya


2 Answers

reassign the replaced string to s

String s="Kathleen D'Souza";
s = s.replaceAll("'","''");
like image 105
codeMan Avatar answered Oct 02 '22 15:10

codeMan


Please try s= "test ' test";

`s.replaceAll("'","\"");`     => test " test

`s.replaceAll("'","''");`     => test '' test
like image 22
Jason Avatar answered Oct 02 '22 14:10

Jason