问题:
I have to use " in a string but apparently, I cant. To be more specific I need to do something like: Let's say the string is something like:
I like cookie's "%s"
str =...
可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:
问题:
I have to use " in a string but apparently, I cant. To be more specific I need to do something like: Let's say the string is something like:
I like cookie's "%s"
str = str.replace('"', '"');
But I need the string to look like this after the replace :
I Like cookie's "%s"
Is anyone willing to help?
回答1:
The problem is that " and / are both special characters. In order to use them as literals in strings, you need to escape them. So the correct way would be:
str = str.replace(""", "\"");
In this context the strings are broken down as such, with spaces around the characters or clarity:
" " " This is an escaped quote
" \ " This is an escaped backslash
Thus if you want to represent a backslash followed by a quote, you can see that is breaks down as:
" \ " " Quotes around the whole literal, an escaped backslash, an escaped quote