从字符串中删除html标记但还原\ n

时间:2014-12-24 17:29:17

标签: android html

在我的新闻应用程序中,我从JSON获取数据,我需要在TextView中显示。 但是来的数据内容与html标签混在一起。 我尝试从字符串中删除html标签,但遇到了一些问题。

这是我有的字符串:

String testString = "<a href=\"#"><img src=\"xyz.jpg" width=\"196\" height=\"300\" /></a>Residents of Chisapani bazaar in the south of Khotang district have been living in darkness for the past three years.\r\n\r\nThe power house of Dobhane river Hydropower Project was damaged in a landslide three years ago.\r\n\r\n Source:ABCD";

我需要在TextView中显示此字符串,不包括html标记。这就是我所做的。

textbox = (TextView) findViewById(R.id.textview1);
textbox.setText(Html.fromHtml(testString).toString());

这样做,甚至省略了\ n(换行符)部分,将整个文本转换为删除段落的单个块。但这不是优先考虑的。我需要将新闻内容的文字写得很好。

有没有什么方法可以删除<a><img>之类的标记,但还原\ n。

1 个答案:

答案 0 :(得分:4)

试试这个

           tv.setText(Html.fromHtml(testString.replace("\n", "<br/>")));

这将在每个“\ n”处创建新行。 但是当你的数据本身包含\ n时,它可能会导致问题......

相关问题