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 - Ebean enhancement ignores a model -

ubuntu - How to disable Kernel Module Signing in linux -

SQL php on different pages to Insert (mysqli) -