0

I am getting a HTML string as a result of querying a remote XML feed. I use the result to set text to a TextView. The problem is that a string contains HTML comment tags which are not supported by the TextView element.

Now, I need a way to remove (substring) the resulting string by indicating the part which will be removed. I cannot work via start and end positions, but I have to use starting and ending string pattern (<!-- as starting and --> as ending).

How can I do this?

sandalone
  • 41,141
  • 63
  • 222
  • 338
  • 2
    post your code, it may help to fix your issue – Aerrow May 04 '12 at 08:31
  • 1
    @DaUltimateTrooper take care when using Regexes on HTML/XML though, only in some special cases they are the right tool (though this might be one). Often you should use a HTML/XML parser. – dtech May 04 '12 at 08:34
  • @Aerrow What code? It's a string I described :). – sandalone May 04 '12 at 08:47

5 Answers5

1

perhaps use this:

String str = "your html string";
int start = str.indexOf("<!--");
int end = str.indexOf("-->");
str = str.replace(str.substring(start, (end - start)), "");
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Better use `int end = str.lastIndexOf("-->");` – J.A.I.L. May 04 '12 at 08:40
  • 2
    nope, i dont think. Because you never know if there are a couple of comments inside html. – waqaslam May 04 '12 at 08:46
  • @Waqas I pointed it out because I thought there could be nested comments[, but I see it's not legal in valid HTMLs](http://stackoverflow.com/questions/442786/are-nested-html-comments-possible). And I wasn't thinking in opening one comment, closing it and oppening (and closing) another comment. – J.A.I.L. May 04 '12 at 10:38
  • @Waqas This code removed everything BUT the each ending "-->". If I try to add + 1 to end, then the app crashes. – sandalone May 04 '12 at 10:54
  • Hm... have you tried this? Now it returns another string `ref; -->`. – sandalone May 04 '12 at 17:48
1

I found this in here. I believe that since android is a tag here the answer will be relevant.

android.text.Html.fromHtml(instruction).toString()

Remove HTML tags from a String.

Community
  • 1
  • 1
Thihara
  • 7,031
  • 2
  • 29
  • 56
1

You can use regular express, e.g.

    String input = "<!-- \nto be removed -->hello <!-- to be removed-->world";
    Pattern pattern = Pattern.compile("<!--.*?-->", Pattern.DOTALL | Pattern.UNICODE_CASE | Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(input);
    StringBuilder builder = new StringBuilder();
    int lastIndex = 0;
    while (matcher.find()) {
        builder.append(input.substring(lastIndex, matcher.start()));
        lastIndex = matcher.end();
    }
    builder.append(input.substring(lastIndex));
    System.out.println(builder);
Qiang Jin
  • 4,427
  • 19
  • 16
0

You can also use HTML.fromHTML

Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
0

You can use the Html.fromHtml() method to use html-formatted text in a TextView, example:

CharSequence text = Html.fromHtml("before <!--comment--><b>after</b>");
myTextView.setText(text);

The TextView will now have the text "before after".

Jave
  • 31,598
  • 14
  • 77
  • 90