在Java中用HTML实体替换字符 [英] Replace characters with HTML entities in java

查看:224
本文介绍了在Java中用HTML实体替换字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某个过滤器中的HTML响应中替换其各自的HTML实体的某些字符。角色包括 > & 。我不能使用 replaceAll(),因为它将替换所有字符,甚至是HTML标签的一部分。

I want to replace certain characters with their respective HTML entities in an HTML response inside a filter. Characters include <, >, &. I can't use replaceAll() as it will replace all characters, even those that are part of HTML tags.

这样做最好的方法是什么?

What is the best approach for doing so?

推荐答案

从java可以尝试,Apache Commons Lang(legacy v2) a href =https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#escapeHtml(java.lang.String) = nofollow noreferrer> StringEscapeUtils.escapeHtml()

或与commons-lang3: StringEscapeUtils.escapeHtml4()

From java you may try, Apache Commons Lang (legacy v2) StringEscapeUtils.escapeHtml().
or with commons-lang3: StringEscapeUtils.escapeHtml4().

请注意,这也会转换à to & agrave; &这样的

Please note this also converts à to &agrave; & such

这篇关于在Java中用HTML实体替换字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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