src/java.base/share/classes/java/lang/String.java
changeset 57783 a0257f1458aa
parent 55290 ceabad800468
child 57956 e0b8b019d2f5
equal deleted inserted replaced
57782:ca133d5ea78a 57783:a0257f1458aa
  1866      * @exception  IndexOutOfBoundsException  if
  1866      * @exception  IndexOutOfBoundsException  if
  1867      *             {@code beginIndex} is negative or larger than the
  1867      *             {@code beginIndex} is negative or larger than the
  1868      *             length of this {@code String} object.
  1868      *             length of this {@code String} object.
  1869      */
  1869      */
  1870     public String substring(int beginIndex) {
  1870     public String substring(int beginIndex) {
  1871         if (beginIndex < 0) {
  1871         return substring(beginIndex, length());
  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     }
  1872     }
  1884 
  1873 
  1885     /**
  1874     /**
  1886      * Returns a string that is a substring of this string. The
  1875      * Returns a string that is a substring of this string. The
  1887      * substring begins at the specified {@code beginIndex} and
  1876      * substring begins at the specified {@code beginIndex} and
  3675      * negative or greater than or equal to {@code length}.
  3664      * negative or greater than or equal to {@code length}.
  3676      */
  3665      */
  3677     static void checkIndex(int index, int length) {
  3666     static void checkIndex(int index, int length) {
  3678         if (index < 0 || index >= length) {
  3667         if (index < 0 || index >= length) {
  3679             throw new StringIndexOutOfBoundsException("index " + index +
  3668             throw new StringIndexOutOfBoundsException("index " + index +
  3680                                                       ",length " + length);
  3669                                                       ", length " + length);
  3681         }
  3670         }
  3682     }
  3671     }
  3683 
  3672 
  3684     /*
  3673     /*
  3685      * StringIndexOutOfBoundsException  if {@code offset}
  3674      * StringIndexOutOfBoundsException  if {@code offset}
  3686      * is negative or greater than {@code length}.
  3675      * is negative or greater than {@code length}.
  3687      */
  3676      */
  3688     static void checkOffset(int offset, int length) {
  3677     static void checkOffset(int offset, int length) {
  3689         if (offset < 0 || offset > length) {
  3678         if (offset < 0 || offset > length) {
  3690             throw new StringIndexOutOfBoundsException("offset " + offset +
  3679             throw new StringIndexOutOfBoundsException("offset " + offset +
  3691                                                       ",length " + length);
  3680                                                       ", length " + length);
  3692         }
  3681         }
  3693     }
  3682     }
  3694 
  3683 
  3695     /*
  3684     /*
  3696      * Check {@code offset}, {@code count} against {@code 0} and {@code length}
  3685      * Check {@code offset}, {@code count} against {@code 0} and {@code length}