Add Strings.substring() that handles short strings.

This commit is contained in:
Andrew Raines 2014-01-07 13:54:20 -06:00
parent 2345e11828
commit da14b5f5e4
1 changed files with 17 additions and 0 deletions

View File

@ -1548,4 +1548,21 @@ public class Strings {
throw new ElasticsearchIllegalStateException("should not be thrown");
}
}
/**
* Return substring(beginIndex, endIndex) that is impervious to string length.
*/
public static String substring(String s, int beginIndex, int endIndex) {
if (s == null) {
return s;
}
int realEndIndex = s.length() > 0 ? s.length() - 1 : 0;
if (endIndex > realEndIndex) {
return s.substring(beginIndex);
} else {
return s.substring(beginIndex, endIndex);
}
}
}