【问题标题】:remove html tags from a string but restore \n从字符串中删除 html 标签但恢复 \n
【发布时间】:2015-02-22 18:40:53
【问题描述】:

在我的新闻应用程序中,我从 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 (换行符)部分,这将整个文本变成了一个单独的块,删除了段落。但这不是首选。我需要有条理的新闻内容文本。

有没有什么方法可以只删除&lt;a&gt;&lt;img&gt; 等标签但恢复\n。

【问题讨论】:

标签: android html


【解决方案1】:

试试这个

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多