2011-09-12 105 views
0

我有一个像提取文本

some text <p>any text</p> 

串从中我需要删除部分

<p>any text</p> 

,结果,得到字符串

some text 

我发现教程中的一些示例代码使用字符串,但我不会rstand如何工作。我是编码方面的新手,很难,因为我不懂英文。

private String description; 

public void setDescription(String description) { 
    this.description = description; 
    if (description.contains("<p>")) { 
     String musor = description.substring(description.indexOf("<p>")); 
     String cleanUp = musor.substring(0, musor.indexOf("</p>")+1); 
     musor = musor.substring(musor.indexOf("<p>")); 
     this.description = this.description.replace(cleanUp, ""); 
    } 
} 
+0

也许它的帮助太android.text.Html.fromHtml(description).toString(); – poucleau

回答

3

你可以使用正则表达式来实现这个功能。

String regexp = "<p>.*?</p>"; 
String replace = ""; 
myString.replaceAll(regexp, replace); 

替换所有<p>标签都有效及其与内容。 (另请参阅http://www.regular-expressions.info/。)

我想有很多库,可以做同样的甚至更多。