這是一種在某些文本中基本上擺脫html標記的方法。方法刪除由以下給出,我測試它,它的工作原理。在方法中提取循環外部的變量
public static String remove(String text, String str) {
int firstIndex = text.indexOf(str);
int beginofNewIndex = (firstIndex + 1) + str.length();
if (firstIndex > 0) {
return text.substring(0, firstIndex) + text.substring(beginofNewIndex);
} else {
return text;
}
}
但是,當我返回下面給出的「文本」時,它給了我與輸入時相同的值。假設方法removeAllTags的參數是String文本。我輸入「< b>這個男孩走了</b>」但它返回相同的東西。有沒有人看到任何錯誤?
public static String removeAllTags(String text) {
int textLength = text.length();
while (textLength > 2) {
int firstIndex = text.indexOf("<");
int secondIndex = text.indexOf(">");
int thirdIndex = text.indexOf("</", secondIndex);
int fourthIndex = text.indexOf(">", secondIndex);
if (firstIndex >= 0 && secondIndex >= 0 && thirdIndex >= 0 && fourthIndex >= 0F) {
remove(text, text.substring(firstIndex, (secondIndex + 1)));
// remove(text, text.substring(thirdIndex, (fourthIndex + 1))); I will implement this into the code but I am testing with the first remove method first.
}
textLength = textLength - 1;
}
return text;
}
你必須這樣做嗎?也許看到這個問題:http://stackoverflow.com/questions/832620/stripping-html-tags-in-java – Catchwa