简体   繁体   English

如何在Java中替换“”字符?

[英]How do i replace " character in Java?

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\\\\";

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. 后者不是格式正确的Java代码。

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\\\\\\""; 字符串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("\\"", "\\\\\\"")); System.out.println(a.replace(“ \\”“,” \\\\\\“”));

Since "String result = a.replace(""", "\\"");" 由于“字符串结果= a.replace(“”“,” \\“”);“ does not compile, does that answer your question? 无法编译,是否可以回答您的问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM