Podejście Jlordo obejmuje określoną sytuację. Jeśli spróbujesz zbudować metodę abstrakcyjną, możesz napotkać trudności, aby sprawdzić, czy "textFrom
" jest przed "textTo
". W przeciwnym razie metoda może zwrócić dopasowanie dla innego wystąpienia "textFrom
" w tekście.
Tutaj jest gotowy do przejść abstrakcyjny sposób, że obejmuje tę wadę:
/**
* Get text between two strings. Passed limiting strings are not
* included into result.
*
* @param text Text to search in.
* @param textFrom Text to start cutting from (exclusive).
* @param textTo Text to stop cuutting at (exclusive).
*/
public static String getBetweenStrings(
String text,
String textFrom,
String textTo) {
String result = "";
// Cut the beginning of the text to not occasionally meet a
// 'textTo' value in it:
result =
text.substring(
text.indexOf(textFrom) + textFrom.length(),
text.length());
// Cut the excessive ending of the text:
result =
result.substring(
0,
result.indexOf(textTo));
return result;
}
Wygląda na to, że wprowadzasz w błąd * dzielenie * ciągu znaków z dopasowaniem do wzorca. –