如何在 Java 中将非特殊字符编码为 HTML 实体
以下代码:
org.apache.commons.lang.StringEscapeUtils.unescapeHtml("Hello World");
给出:
Hello World
但我想知道如何从“Hello World”返回解码后的字符串。我尝试过 escapeHtml 方法,但这只编码特殊字符。
The following code:
org.apache.commons.lang.StringEscapeUtils.unescapeHtml("Hello World");
gives:
Hello World
But I'd like to know how to get back to the decoded string from "Hello World". I have tried the escapeHtml method, but this only encodes special characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于“任何有用的东西”不正确;如果您的测试字符串包含 HTML 特殊字符,例如 <、>、&,该函数会将其转换为 < >和&; (并将其他上层ISO8859-1代码更改为实体)。
如果您需要将其编码回 Unicode 实体格式,只需迭代字符串代码点即可:
Not true about "anything useful"; if your test string contained HTML special characters like <,>,&, the function would've turned it into < > and & (and change other upper ISO8859-1 codes into entities).
If you need to encode it back to Unicode entity format, just iterate through the String codepoints: