【发布时间】:2016-05-08 08:09:31
【问题描述】:
Android 有两种不同的方式来转义/编码字符串中的 HTML 字符/实体:
-
Html.escapeHtml(String),在 API 16 (Android 4.1) 中添加。文档说:返回给定纯文本的 HTML 转义表示。
-
TextUtils.htmlEncode(String)对于这个,文档说:对字符串进行 HTML 编码。
阅读文档,他们似乎都在做几乎相同的事情,但是,在测试它们时,我得到了一些非常神秘的(对我而言)输出。
例如。输入:<p>This is a quote ". This is a euro symbol: €. <b>This is some bold text</b></p>
-
Html.escapeHtml给出:<p>This is a quote ". This is a euro symbol: €. <b>This is some bold text</b></p> -
而
TextUtils.htmlEncode给出:<p>This is a quote ". This is a euro symbol: €. <b>This is some bold text</b></p>
所以似乎第二个转义/编码引号(“),但第一个没有,虽然第一个编码欧元符号,但第二个没有。我是困惑。
那么这两种方法有什么区别呢?每个转义/编码哪些字符? encoding 和 escaping 这里有什么区别?我什么时候应该使用其中一种(或者我应该,喘不过气来,同时使用它们?)?
【问题讨论】:
标签: android android-webview html-entities html-encode html-escape