< prev index next >
src/java.base/share/classes/java/lang/String.java
Print this page
rev 55936 : [mq]: 8221307-String-substring-OOB-exception-on-start-index-reports-improper-information
@@ -1866,20 +1866,21 @@
* @exception IndexOutOfBoundsException if
* {@code beginIndex} is negative or larger than the
* length of this {@code String} object.
*/
public String substring(int beginIndex) {
- if (beginIndex < 0) {
- throw new StringIndexOutOfBoundsException(beginIndex);
- }
int subLen = length() - beginIndex;
- if (subLen < 0) {
- throw new StringIndexOutOfBoundsException(subLen);
- }
+ if (beginIndex <= 0 || subLen <= 0) {
if (beginIndex == 0) {
return this;
}
+ if (subLen == 0) {
+ return "";
+ }
+ throw new StringIndexOutOfBoundsException(
+ "begin " + beginIndex + ", length " + length());
+ }
return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
: StringUTF16.newString(value, beginIndex, subLen);
}
/**
< prev index next >