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
Post a Comment