How can I replace string containing " with \" ?
replace(""","\"") is not working for me.
public static String replaceSpecialCharsForJson(String str){
return str.replace("'","\'")
.replace(":","\\:")
.replace("\"","\"")
.replace("\r", "\\r")
.replace("\n", "\\n");
}
You can try with:
replace("\"","\\\"")
Since both " and \ are metacharacters, you have to escape them with \
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