So Trivial: Write "" in Java
This is the most trivial question but I cannot figure out how to solve this.
In a String I want to relace all the occurences of this string " with this ""
My problem is that the compiler wont allow me to write """" or '""' (hope this makes sense).
开发者_如何学GoSo my code is:
s = s.replace('"', '""'); // so the 2nd parameter of the method is causing an error
I hope this makes sense :P
EDIT: (using replace instead of replaceAll per suggestion)
s = s.replace("\"", "\"\"");
StackOverflowException's answer is correct. you need to escape quotes with a blackslash
keep in mind that (unlike some other languages) single quotes are for single characters and double quotes are for strings of 0 or more characters
e.g.
'a' 'b' '1' '\"'
"" "a" "this is a string"
精彩评论