0

in my app I get a String from RSS fedd item(tag description)and put it in a ListView but the String looks like this:

enter image description here

I try to remove the tag with this method but doesn't work:

private String refactor(String mess) {
        int indexToRemove = mess.lastIndexOf("<strong>");
        String toBeReplaced = mess;
        if(indexToRemove >= 0 ){
             toBeReplaced = mess.substring(0, indexToRemove);
        }
        int newIndex = toBeReplaced.lastIndexOf("</strong>");
        String messFinal = toBeReplaced;
        if(newIndex >= 0){
            messFinal = toBeReplaced.substring(0, newIndex);
        }
        return messFinal;
    }
Marduk
  • 125
  • 2
  • 14

4 Answers4

3

Would be better to not to remove HTML tags, but render them correctly.

textView.setText(Html.fromHtml(sourceString));

Veaceslav Gaidarji
  • 4,261
  • 3
  • 38
  • 61
0

Why don't just use replace() from String?

https://www.tutorialspoint.com/java/java_string_replace.htm

Victor Hugo Montes
  • 1,270
  • 1
  • 17
  • 28
0

If you want render the HTML correctly you could just use

Spanned parsedHtmlString = Html.fromHtml(htmlString)

instaed if rendering the html can cause you some layout problems you can use this to remove the html tags

Spanned strippedHtmlString = Html.fromHtml(htmlString).toString()

Remember that those metdods are deprecated starting from Android N, so you need to use something like this for compiling with API 24+

Spanned result;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    result = Html.fromHtml(htmlString, Html.FROM_HTML_MODE_LEGACY); //add .toString() to remove html tags
} else {
    result = Html.fromHtml(htmlString); //add .toString() to remove html tags
}

took from here

EDIT Based on your comment, i think you should go with this:

public RssItem(Parcel source) { 
    Bundle data = source.readBundle();
    title = Html.fromHtml(data.getString("title")).toString(); 
    link = Html.fromHtml(data.getString("link")).toString(); 
    pubDate = (Date) data.getSerializable("pubDate"); 
    description = Html.fromHtml(data.getString("description")).toString(); 
    content = Html.fromHtml(data.getString("content")).toString(); 
    feed = data.getParcelable("feed"); 
}

i've omitted some objects because it really depends on what they contain. Moreover you should do some check for null before parsing them with Html.fromHtml

Community
  • 1
  • 1
MatPag
  • 41,742
  • 14
  • 105
  • 114
  • I get item from rss fedd – Marduk Oct 21 '16 at 07:55
  • I get rss fedd item with this method public RssItem(Parcel source) { Bundle data = source.readBundle(); title = data.getString("title"); link = data.getString("link"); pubDate = (Date) data.getSerializable("pubDate"); description = data.getString("description"); content = data.getString("content"); feed = data.getParcelable("feed"); } And 'here that I have to use the method Html.fromHtml? – Marduk Oct 21 '16 at 07:57
  • @Marduk i've updated my answer above – MatPag Oct 21 '16 at 13:47
0

You can use Jsoup for removing html tags.

private String refactor(String html) {
    return Jsoup.parse(html).text();
}
Caner Balım
  • 556
  • 9
  • 21