regex - Java string how to replace " with \" to escape JSON? -


this question has answer here:

i trying replace " \" in java, slashes getting confusing. proper way replace " \" in java?

string.replaceall("\"","\\""); 

if going replace literals don't use replaceall replace.

reason replaceall uses regex syntax means characters treated specially + * \ ( ) , make them literals need escape them. replace adds escaping mechanism automatically, instead of

replaceall("\"", "\\\\\"") 

you can write

replace("\"", "\\\""); 

which little less confusing.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -