< 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


1851     }
1852 
1853     /**
1854      * Returns a string that is a substring of this string. The
1855      * substring begins with the character at the specified index and
1856      * extends to the end of this string. <p>
1857      * Examples:
1858      * <blockquote><pre>
1859      * "unhappy".substring(2) returns "happy"
1860      * "Harbison".substring(3) returns "bison"
1861      * "emptiness".substring(9) returns "" (an empty string)
1862      * </pre></blockquote>
1863      *
1864      * @param      beginIndex   the beginning index, inclusive.
1865      * @return     the specified substring.
1866      * @exception  IndexOutOfBoundsException  if
1867      *             {@code beginIndex} is negative or larger than the
1868      *             length of this {@code String} object.
1869      */
1870     public String substring(int beginIndex) {
1871         if (beginIndex < 0) {
1872             throw new StringIndexOutOfBoundsException(beginIndex);
1873         }
1874         int subLen = length() - beginIndex;
1875         if (subLen < 0) {
1876             throw new StringIndexOutOfBoundsException(subLen);
1877         }
1878         if (beginIndex == 0) {
1879             return this;






1880         }
1881         return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
1882                           : StringUTF16.newString(value, beginIndex, subLen);
1883     }
1884 
1885     /**
1886      * Returns a string that is a substring of this string. The
1887      * substring begins at the specified {@code beginIndex} and
1888      * extends to the character at index {@code endIndex - 1}.
1889      * Thus the length of the substring is {@code endIndex-beginIndex}.
1890      * <p>
1891      * Examples:
1892      * <blockquote><pre>
1893      * "hamburger".substring(4, 8) returns "urge"
1894      * "smiles".substring(1, 5) returns "mile"
1895      * </pre></blockquote>
1896      *
1897      * @param      beginIndex   the beginning index, inclusive.
1898      * @param      endIndex     the ending index, exclusive.
1899      * @return     the specified substring.




1851     }
1852 
1853     /**
1854      * Returns a string that is a substring of this string. The
1855      * substring begins with the character at the specified index and
1856      * extends to the end of this string. <p>
1857      * Examples:
1858      * <blockquote><pre>
1859      * "unhappy".substring(2) returns "happy"
1860      * "Harbison".substring(3) returns "bison"
1861      * "emptiness".substring(9) returns "" (an empty string)
1862      * </pre></blockquote>
1863      *
1864      * @param      beginIndex   the beginning index, inclusive.
1865      * @return     the specified substring.
1866      * @exception  IndexOutOfBoundsException  if
1867      *             {@code beginIndex} is negative or larger than the
1868      *             length of this {@code String} object.
1869      */
1870     public String substring(int beginIndex) {



1871         int subLen = length() - beginIndex;
1872         if (beginIndex <= 0 || subLen <= 0) {


1873             if (beginIndex == 0) {
1874                 return this;
1875             }
1876             if (subLen == 0) {
1877                 return "";
1878             }
1879             throw new StringIndexOutOfBoundsException(
1880                 "begin " + beginIndex + ", length " + length());
1881         }
1882         return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
1883                           : StringUTF16.newString(value, beginIndex, subLen);
1884     }
1885 
1886     /**
1887      * Returns a string that is a substring of this string. The
1888      * substring begins at the specified {@code beginIndex} and
1889      * extends to the character at index {@code endIndex - 1}.
1890      * Thus the length of the substring is {@code endIndex-beginIndex}.
1891      * <p>
1892      * Examples:
1893      * <blockquote><pre>
1894      * "hamburger".substring(4, 8) returns "urge"
1895      * "smiles".substring(1, 5) returns "mile"
1896      * </pre></blockquote>
1897      *
1898      * @param      beginIndex   the beginning index, inclusive.
1899      * @param      endIndex     the ending index, exclusive.
1900      * @return     the specified substring.


< prev index next >