【发布时间】:2011-05-08 14:02:23
【问题描述】:
在打印出来之前,我需要从字符串中转义引号。
我编写了一个函数,使用 char 数组尽可能明确。但是这个函数似乎只是打印出它自己的输入:
public static String escapeQuotes(String myString) {
char[] quote=new char[]{'"'};
char[] escapedQuote=new char[]{'\\' , '"'};
return myString.replaceAll(new String(quote), new String(escapedQuote));
}
public static void main(String[] args) throws Exception {
System.out.println("asd\"");
System.out.println(escapeQuotes("asd\""));
}
我希望它的输出是: ” asd\"
但是我得到的是: ” asd"
任何想法如何正确地做到这一点?
谢谢
【问题讨论】: