本文介绍了StringEscapeUtils 找出字符串是否被转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 StringEscapeUtils.escapeHTML 来转义 URL.是否有类似的东西可以找出字符串是否已经转义?

I've been using StringEscapeUtils.escapeHTML to escape URLs. Is there something similar to find out if the string is already escaped?

推荐答案

我不知道,但自己做一个很容易:

Not that I know of, but it is pretty easy to do one yourself:

public boolean isEscaped(String url) {
    return !url.equals(StringEscapeUtils.unEscapeHTML(url));
}

请注意,正如@themel 指出的那样,决定是否对随机字符串进行转义是不可能的,如果您对随机字符串进行尝试,可能会得到很多误报.但是,我假设您至少可以控制您的字符串在此处的外观.

Note that deciding if a random string is escaped or not is impossible as @themel notes, you can get a lot of false positives if you try this with random strings. However I'm assuming that you at least have some control over what your strings look like here.

这篇关于StringEscapeUtils 找出字符串是否被转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:13