Reputation: 41769
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?
Upvotes: 0
Views: 1052
Reputation: 68187
perhaps use this:
String str = "your html string";
int start = str.indexOf("<!--");
int end = str.indexOf("-->");
str = str.replace(str.substring(start, (end - start)), "");
Upvotes: 1
Reputation: 31856
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".
Upvotes: 0
Reputation: 4467
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);
Upvotes: 1
Reputation: 6969
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.
Upvotes: 1