我有以下HTML:
<p>Bla bla bla...</p>
<p><strong>4. others</strong></p>
<p> </p>它包含一个随机的<p> </p>标签组合,需要在我的安卓应用程序中过滤掉。为此,我使用了以下Java代码:
String html = object.get("Content").toString(); // this is the HTML
html = html.replace("<p> </p>", "");
html = html.replace("<p></p>", "");
html = html.replace("<p><span></span></p>", "");
content.setText(Html.fromHtml(html));但是,当我调试并在替换函数上放置一个断点时,它不会替换字符串。现在我有我不想要的无用的<p>标签。我该怎么解决这个问题?
发布于 2015-07-15 10:38:23
它包含一个随机的
标签组合,需要在我的Android应用程序中过滤掉。您需要分别替换它,。因为替换()函数可以工作case-sensitive和顺序字符串。所以,使用下面的代码
String html = object.get("Content").toString().trim(); // this is the HTML
html = html.replace("<p>", "");
html = html.replace("</p>", "");
html = html.replace("<span>", "");
html = html.replace("</span>", "");
content.setText(Html.fromHtml(html));而不是替换()中的连接字符串
String html = object.get("Content").toString(); // this is the HTML
html = html.replace("<p> </p>", "");
html = html.replace("<p></p>", "");
html = html.replace("<p><span></span></p>", "");
content.setText(Html.fromHtml(html));你会得到你想要的结果。
注意事项:但是,请记住,任何时候您都想替换任何字符/字符串,而不是按顺序替换为区分大小写的字符串。因为它给了你准确的结果。
发布于 2015-07-15 10:41:11
你这样做是不对的。
正确的方法: 1.使用StringBuffer而不是字符串。2.查找<的索引并将其放入变量中,如int startIdx 3,查找索引of >并将其放入变量( int endIdx 4 )中,然后使用StringBuffer的delete()并指定此startIdx和endIdx来删除startIdx标记。
这将从字符串中删除所有HTML代码。
Ok --根据你的评论,这是你怎么做的--
String replaceThis="<p></p>";
int len = replaceThis.length();
StringBuffer buff="your html string from which you want to replace";
int i;
while((i = buff.indexOf(replaceThis)!=-1)
{
buff.delete(i,len-1);
}https://stackoverflow.com/questions/31427802
复制相似问题