Это метод, чтобы в основном избавиться от html-тегов в некотором тексте. метод remove задается следующим, и я тестировал его, и он работает.Извлечение переменной за пределами цикла в методе
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 является строковым текстом. Я ввожу «< 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