番石榴是否提供解串字符串的方法? [英] Does Guava provide a method to unescape a String?

查看:66
本文介绍了番石榴是否提供解串字符串的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在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('<', "&lt;")
        .addEscape('>', "&gt;")
        .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:

我不太清楚无法转义的用例.通常不是 甚至可以在没有解析器的情况下识别转义的源文本 懂语言.例如,如果我有以下内容 输入:

The use case for unescaping is less clear to me. It's generally not possible to even identify the escaped source text without a parser that understands the language. For example, if I have the following input:

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.

这篇关于番石榴是否提供解串字符串的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆