Jlordo 方法涵盖特定情况。如果您尝试从中构建抽象方法,则可能难以检查“ textFrom
”是否在“ textTo
”之前。textFrom
否则,方法可以返回匹配文本中出现的其他一些 ' ' 的匹配项。
这是一个现成的抽象方法,可以弥补这个缺点:
/**
* 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;
}