Operations on {@link java.lang.String} that are
* null
safe.
Gets len
characters from the middle of a String.
If len
characters are not available, the remainder
* of the String will be returned without an exception. If the
* String is null
, null
will be returned.
* An exception is thrown if len is negative.
* StringUtils.mid(null, *, *) = null * StringUtils.mid(*, *, -ve) = "" * StringUtils.mid("", 0, *) = "" * StringUtils.mid("abc", 0, 2) = "ab" * StringUtils.mid("abc", 0, 4) = "abc" * StringUtils.mid("abc", 2, 4) = "c" * StringUtils.mid("abc", 4, 2) = "" * StringUtils.mid("abc", -2, 2) = "ab" ** * @param str the String to get the characters from, may be null * @param pos the position to start from, negative treated as zero * @param len the length of the required String, must be zero or positive * @return the middle characters,
null
if null String input
*/
public static String mid(String str, int pos, int len) {
if (str == null) {
return null;
}
if (len < 0 || pos > str.length()) {
return "";
}
if (pos < 0) {
pos = 0;
}
if (str.length() <= (pos + len)) {
return str.substring(pos);
}
return str.substring(pos, pos + len);
}
}