2011-09-12 39 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/。)

我想有很多庫,可以做同樣的甚至更多。