[英]Remove a part of string via first and last character?
I am getting a HTML string as a result of querying a remote XML feed. 查询远程XML提要后得到HTML字符串。 I use the result to set text to a TextView
. 我使用结果将文本设置为TextView
。 The problem is that a string contains HTML comment tags which are not supported by the TextView
element. 问题是字符串包含TextView
元素不支持的HTML注释标签。
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? 我怎样才能做到这一点?
perhaps use this: 也许使用这个:
String str = "your html string";
int start = str.indexOf("<!--");
int end = str.indexOf("-->");
str = str.replace(str.substring(start, (end - start)), "");
I found this in here. 我在这里找到的。 I believe that since android is a tag here the answer will be relevant. 我相信,由于android是此处的标记,因此答案将是相关的。
android.text.Html.fromHtml(instruction).toString()
You can use regular express, eg 您可以使用正则快递,例如
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);
您也可以使用HTML.fromHTML
You can use the Html.fromHtml()
method to use html-formatted text in a TextView
, example: 您可以使用Html.fromHtml()
方法在TextView
使用html格式的文本,例如:
CharSequence text = Html.fromHtml("before <!--comment--><b>after</b>");
myTextView.setText(text);
The TextView will now have the text "before after ". 现在,TextView的文本为“ before after ”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.