问题描述
我有这个简单的问题:一旦我检索邮件文本,有时会发生 Html.fromHtml
无法正确解析字符串的问题。
I have this simple problem: once I retrieve a mail text, sometimes it happens that Html.fromHtml
cannot parse correctly the string.
我给你举个例子。这是HTML字符串:
I'll give you an example. This is the HTML string:
––––&
它需要如下所示:
---------------------------
–––––––––––––––––––––––––––
在Android中有没有办法实现这一目标?我是否需要使用正则表达式?
Is there a way in Android to achieve that? Do I need to use Regular Expressions?
非常感谢您。
Thank you so much.
推荐答案
您可以用以下方式过滤出隐藏的字符(在这种情况下):
You can filter out the hidden characters (in this case) with:
myString = myString.replaceAll( "[\\u202C\\u202A]", "" );
之后,它只是:
After that it's just:
Html.fromHtml(myString);
它可以在html上下文中运行。或者,如果你想要真正的em破折号字符:
And it will work in html context. Or if you want the real em dash characters:
Html.fromHtml(Html.fromHtml(myString));
概念的演示:(javascript,你必须在Java的这个答案中使用代码)
Demo of the concept: http://jsfiddle.net/CGzDc/ (javascript, you will have to use code in this answer for java)
这篇关于HTML解析Android中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!