Java 向字符串添加转义参数无效

Java 向字符串添加转义参数无效,java,Java,字符串的当前值为 ^I am Shaikh with "([^"]*)" and "([^"]*)"$ 我的代码如下: System.out.println(strAnnotationValue); // prints ^I am Shaikh with "([^"]*)" and "([^"]*)"$ strAnnotationValue = strAnnotationValue.replaceAll("\"", "\\\""); System.out.println(strAnnotat

字符串的当前值为

^I am Shaikh with "([^"]*)" and "([^"]*)"$
我的代码如下:

System.out.println(strAnnotationValue); // prints ^I am Shaikh with "([^"]*)" and "([^"]*)"$

strAnnotationValue = strAnnotationValue.replaceAll("\"", "\\\"");
System.out.println(strAnnotationValue); 

Actual Output:   ^I am Shaikh with "([^"]*)" and "([^"]*)"$
Expected Output: ^I am Shaikh with \"([^\"]*)\" and \"([^\"]*)\"$


我已经编写了正确的代码,但它不起作用,还有其他方法吗?

如果您的代码是完美的,那么您将获得正确的预期输出

做一件事来取代你的这条线

strAnnotationValue = strAnnotationValue.replaceAll("\"", "\\\"");
用这个

strAnnotationValue = strAnnotationValue.replaceAll("\"", "\\\\\"");
\\\”
表示
\”
,当它输出时,会显示:

这样做:
\\\\”

\\\\”
表示
\\”
,当它输出时,它将被显示:
\“


只是一个友好的通知:如果你的代码不起作用,很可能是你写的代码不正确。看看下面的答案谢谢你的解释星:)
strAnnotationValue = strAnnotationValue.replaceAll("\"", "\\\\\"");