How not to use escape character in java

问题: 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
  • 发表于 2019-07-07 22:11
  • 阅读 ( 152 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除