I have
String a = "data=\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"1\"1\"1\"1\"1开发者_如何学JAVA\\\\";
How can i replace
"
to\"
- and
\
to\\
?
String result = a.replace("\"", "\\\"");
OR
String result = a.replace(""", "\"");
String result = a.replace("\\","\\\\").replace("\"", "\\\"");
This would first replace all \
with \\
and then all "
with \"
if that is what you want.
Note that doing it the other way round would result in "
being replaced with \\"
in the end, since first it get replaced with \"
and then the \
would be replaced with \\
resulting in \\"
.
Additional note: your data string is not well-formed and should not compile: it ends in \"
which is not a valid string literal delimiter (the literal ends in \\\\\"
which would be the string data \\"
) - change that to an even number of slashes or add another "
to the end in order to fix that.
The former. The latter is not well-formed Java code.
Since "String result = a.replace(""", "\"");" does not compile, does that answer your question?
you a string error,Less a quote
String a = "data=\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"0\"1\"1\"1\"1\"0\"0\"0\"0\"0\"1\"1\"1\"1\"1\\\"";
System.out.println(a.replace("\"", "\\\""));
精彩评论