I want to replace all single quotes with escaped single quotes. First I have tried with String.replaceAll("\'", "\\'")
but it didn't worked. So, I have tried with StringUtils.replace(String, "\'", "\\'")
which worked.
Here is the code:
String innerValue = "abc'def'xyz";
System.out.println("stringutils output:"+StringUtils.replace(innerValue, "\'", "\\'"));
System.out.println("replaceAll output:"+innerValue.replaceAll("\'", "\\'"));
Expected Output:
stringutils output:abc\'def\'xyz
replaceAll output:abc\'def\'xyz
Actual Output:
stringutils output:abc\'def\'xyz
replaceAll output:abc'def'xyz
I am just curious Why String.replaceAll
is not replacing '
with \'
?