开发者

So Trivial: Write "" in Java

开发者 https://www.devze.com 2023-02-13 08:10 出处:网络
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 \"\"

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).

开发者_如何学Go

So 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"
0

精彩评论

暂无评论...
验证码 换一张
取 消