Operations on {@link java.lang.String} that are
* null
safe.
Finds the first index within a String, handling null
.
* This method uses {@link String#indexOf(String, int)}.
A null
String will return -1
.
* A negative start position is treated as zero.
* An empty ("") search String always matches.
* A start position greater than the string length only matches
* an empty search String.
* StringUtils.indexOf(null, *, *) = -1 * StringUtils.indexOf(*, null, *) = -1 * StringUtils.indexOf("", "", 0) = 0 * StringUtils.indexOf("aabaabaa", "a", 0) = 0 * StringUtils.indexOf("aabaabaa", "b", 0) = 2 * StringUtils.indexOf("aabaabaa", "ab", 0) = 1 * StringUtils.indexOf("aabaabaa", "b", 3) = 5 * StringUtils.indexOf("aabaabaa", "b", 9) = -1 * StringUtils.indexOf("aabaabaa", "b", -1) = 2 * StringUtils.indexOf("aabaabaa", "", 2) = 2 * StringUtils.indexOf("abc", "", 9) = 3 ** * @param str the String to check, may be null * @param searchStr the String to find, may be null * @param startPos the start position, negative treated as zero * @return the first index of the search String, * -1 if no match or
null
string input
* @since 2.0
*/
public static int indexOf(String str, String searchStr, int startPos) {
if (str == null || searchStr == null) {
return -1;
}
// JDK1.2/JDK1.3 have a bug, when startPos > str.length for "", hence
if (searchStr.length() == 0 && startPos >= str.length()) {
return str.length();
}
return str.indexOf(searchStr, startPos);
}
}