2011-02-23 59 views
-1

這是最微不足道的問題,但我無法弄清楚如何解決這個問題。所以平凡:在Java中寫「012」

在一個字符串我想relace這個字符串的所有OCCURENCES「這個‘’

我的問題是,編譯器不會讓我寫‘’」」或‘‘’’(希望這。是有道理的)

所以我的代碼是:

s = s.replace('"', '""'); // so the 2nd parameter of the method is causing an error 

我希望這是有道理的:P

回答

8

編輯:(使用替換而不是每個建議的replaceAll)

s = s.replace("\"", "\"\""); 
+0

太複雜 - 爲什麼要在這裏使用正則表達式? 's = s.replace(「\」「,」\「\」「);'肯定快得多。 – maaartinus 2011-02-23 05:22:17

1

StackOverflowException的答案是正確的。你需要逃跑報價與blackslash

記住,(其它語言不同)單引號是單個字符 和雙引號是0個或更多字符

例如串

'a' 'b' '1' '\"' 
"" "a" "this is a string"