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

查看:19
本文介绍了用 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) StringEscapeUtils.escapeHtml().或者使用 commons-lang3: StringEscapeUtils.escapeHtml4().

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

请注意,这也会将 à 转换为 &agrave; &

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

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

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