如何在HTML内容中编码特殊字符 [英] How to encode special chars in html content

查看:94
本文介绍了如何在HTML内容中编码特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,是否存在第三方来源或快速命令来将字符串中的html特殊字符转换为html编码的内容?

In Java, Is there a third party source available or quick command to convert html special chars in a string to html encoded content?

例如:

Original code:     <>&abcdef ©
After encoding:    &lt;&gt;&amp;abcdef&copy;

推荐答案

如果要将字符串转换为HTML实体以快速进行测试,则可以使用如下所示的Web服务:

If you want to convert a string to HTML entities to test something quickly, you can use webservices like this one:

http://www.primitivetype.com/resources/htmlentities.php

对于Java,您可以使用Apache Commons Lang中的StringEscapeUtils.看到这个线程:在Java中转义HTML的推荐方法

For Java you can use the StringEscapeUtils from Apache Commons Lang. See this thread: 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);

我从上述线程中借用了示例.

I borrowed the example from the thread mentioned above.

这篇关于如何在HTML内容中编码特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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