这是最微不足道的问题,但我无法弄清楚如何解决这个问题。所以平凡:在Java中写“012”
在一个字符串我想relace这个字符串的所有OCCURENCES“这个‘’
我的问题是,编译器不会让我写‘’””或‘‘’’(希望这。是有道理的)
所以我的代码是:
s = s.replace('"', '""'); // so the 2nd parameter of the method is causing an error
我希望这是有道理的:P
这是最微不足道的问题,但我无法弄清楚如何解决这个问题。所以平凡:在Java中写“012”
在一个字符串我想relace这个字符串的所有OCCURENCES“这个‘’
我的问题是,编译器不会让我写‘’””或‘‘’’(希望这。是有道理的)
所以我的代码是:
s = s.replace('"', '""'); // so the 2nd parameter of the method is causing an error
我希望这是有道理的:P
编辑:(使用替换而不是每个建议的replaceAll)
s = s.replace("\"", "\"\"");
StackOverflowException的答案是正确的。你需要逃跑报价与blackslash
记住,(其它语言不同)单引号是单个字符 和双引号是0个或更多字符
例如串
'a' 'b' '1' '\"'
"" "a" "this is a string"
太复杂 - 为什么要在这里使用正则表达式? 's = s.replace(“\”“,”\“\”“);'肯定快得多。 – maaartinus 2011-02-23 05:22:17