问题描述
我需要在String
中转义特殊字符.
I need to escape special characters in a String
.
Guava提供了 Escaper
类,它正是这样做的:
Guava provides the Escaper
class, which does exactly this:
Escaper escaper = Escapers.builder()
.addEscape('[', "\\[")
.addEscape(']', "\\]")
.build();
String escapedStr = escaper.escape("This is a [test]");
System.out.println(escapedStr);
// -> prints "This is a \[test\]"
现在我已经逃脱了String
,我需要取消转义,并且在Guava中找不到任何东西可以做到这一点.
Now that I have an escaped String
, I need to unescape it and I can't find anything in Guava to do this.
我期望Escaper
具有unescape()
方法,但事实并非如此.
I was expecting Escaper
to have a unescape()
method, but it isn't the case.
我知道,进行转义可能很棘手,在某些无意义的情况下甚至是不可能的.
Edit : I'm aware that unescaping can be tricky, even impossible in some non-sense cases.
例如,这种Escaper
用法可能导致歧义:
For example, this Escaper
usage can lead to ambiguities :
Escaper escaper = Escapers.builder()
.addEscape('@', " at ")
.addEscape('.', " dot ")
.build();
除非转义的数据仅包含电子邮件地址,仅此而已,否则您无法通过将其转义来安全地恢复数据.
Unless the escaped data contains only email addresses and nothing more, you can't safely get your data back by unescaping it.
HTML实体是安全使用Escaper
的一个很好的例子:
A good example of a safe usage of the Escaper
is HTML entities :
Escaper escaper = Escapers.builder()
.addEscape('&', "&")
.addEscape('<', "<")
.addEscape('>', ">")
.build();
在这里,由于涵盖了所有可能的歧义,因此您可以安全地转义任何文本,将其合并到HTML页面中并取消转义以显示它.
Here, you can safely escape any text, incorporate it in a HTML page and unescape it at any time to display it, because you covered every possible ambiguities.
总而言之,我不明白为什么逃避这么有争议.我认为正确使用此类,了解他的数据并避免歧义是开发人员的责任.根据定义,转义意味着您最终将需要逃脱.否则,这是一种混淆或其他概念.
In conclusion, I don't see why unescaping is so controversial. I think it is the developper's responsability to use this class properly, knowing his data and avoiding ambiguities.Escaping, by definition, means you will eventually need to unescape. Otherwise, it's obfuscation or some other concept.
推荐答案
不,不是.显然,这是故意的.引用此讨论,克里斯·波维尔克(Chris Povirk)回答:
No, it does not. And apparently, this is intentional. Quoting from this discussion where Chris Povirk answered:
String s = "foo\n\"bar\"\n\\";
然后我的解析器必须已经理解\n
,\"
和\\
才能 确定...
Then my parser has to already understand \n
, \"
, and \\
in order to identify that...
foo\n\"bar\"\n\\
...是要转义"的文本.换句话说,它必须 已经逃脱.这种情况与HTML和其他类似 格式:我们不需要解析器,只需要解析器即可.
...is the text to be "unescaped." In other words, it has to do the unescaping already. The situation is similar with HTML and other formats: We don't need an unescaper so much as we need a parser.
因此,您似乎必须自己做.
So it looks like you'll have to do it yourself.
这篇关于番石榴是否提供解串字符串的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!