在 Java 中,是否有可用的第三方源或快速命令将字符串中的 html 特殊字符转换为 html 编码内容?
例如:
Original code: <>&abcdef ©
After encoding: <>&abcdef©
最佳答案
如果您想将字符串转换为 HTML 实体以快速测试某些东西,您可以使用像这样的网络服务:
http://www.primitivetype.com/resources/htmlentities.php
[编辑] 对于 Java,您可以使用 Apache Commons Lang 中的 StringEscapeUtils。看到这个线程: Recommended method for escaping HTML in Java
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
// ...
String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML";
String escaped = escapeHtml(source);
我借用了上面提到的线程的例子。
关于java - 如何对html内容中的特殊字符进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5741311/