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>
所以似乎第二个转义/编码引号("),但第一个没有,虽然第一个编码欧元符号,但第二个没有。我很困惑。
那么这两种方法有什么区别呢?每个转义/编码哪些字符?这里的编码和转义有什么区别?我什么时候应该使用其中一种(或者我应该,喘着粗气,同时使用它们?)?