subString源码分析

1. Public String subString(int beginIndex)

String的共有方法,从beginIndex位置开始截取字符串到源字符串末尾,包括beginIndex

"123".subString(1)--->"23"

    /**返回目标String中截取的指定String
     * Returns a string that is a substring of this string. The
     * substring begins with the character at the specified index and
     * extends to the end of this string. <p>
     * Examples:
     * <blockquote><pre>
     * "unhappy".substring(2) returns "happy"
     * "Harbison".substring(3) returns "bison"
     * "emptiness".substring(9) returns "" (an empty string)
     * </pre></blockquote>
     *
     * @param      beginIndex   the beginning index, inclusive.    开始位(包括在结果中)
     * @return     the specified substring.               指定的Strng
     * @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) {                      //当开始位beginIndex小于0抛出异常StringIndexOutOfBoundsException()
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = value.length - beginIndex;
        if (subLen < 0) {                        //要截取的String的长度小于0时抛出异常StringIndexOutOfBoundsException()
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);  //当开始位beginIndex等于0时相当于返回字符串本身否则返回新的String
    }

 

2.public String substring(int beginIndex, int endIndex)

String的共有方法,从beginIndex位置开始截取字符串到endIndex,包括beginIndex,但是不包括endIndex

"123".subString(1,2)--->"2"

/**
     * Returns a string that is a substring of this string. The
     * substring begins at the specified {@code beginIndex} and
     * extends to the character at index {@code endIndex - 1}.
     * Thus the length of the substring is {@code endIndex-beginIndex}.
     * <p>
   * 返回一个从本字符串截取的新的字符串 * Examples: * <blockquote><pre> * "hamburger".substring(4, 8) returns "urge" * "smiles".substring(1, 5) returns "mile" * </pre></blockquote> * *
@param beginIndex the beginning index, inclusive.  开始截取位置(包括在内) * @param endIndex the ending index, exclusive.    结束位置(不包括在内) * @return the specified substring. * @exception IndexOutOfBoundsException if the * {@code beginIndex} is negative, or * {@code endIndex} is larger than the length of * this {@code String} object, or * {@code beginIndex} is larger than * {@code endIndex}. */ public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) {                        //开始位置小于0时抛出异常 throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > value.length) {                   //结束位置大于本字符串长度时抛出异常(String通过char[] value储存) throw new StringIndexOutOfBoundsException(endIndex); } int subLen = endIndex - beginIndex; if (subLen < 0) {                          //截取长度小于0时抛出异常 throw new StringIndexOutOfBoundsException(subLen); } return ((beginIndex == 0) && (endIndex == value.length)) ? this  //当开始位置等与0且结束位置等于字符串长度时返回本身,否则新建一个String返回 : new String(value, beginIndex, subLen); }

3.public String(char value[],int offset,int count)

String的构造方法,根据char[]value数组作为源数组,新建一个String,从第offset位置开始,一共截取count个字符,结果包括offset

String test=new String(new char[]{'1','2','3'},1,2);
test------>23
/**
     * Allocates a new {@code String} that contains characters from a subarray
     * of the character array argument. The {@code offset} argument is the
     * index of the first character of the subarray and the {@code count}
     * argument specifies the length of the subarray. The contents of the
     * subarray are copied; subsequent modification of the character array does
     * not affect the newly created string.
   * 分配一个新的String,从一个char[]数组截取(String类带有char[] value成员,它是String的主要存储结构),
   * 参数offset是开始复制的位置,count是复制的长度大小 * *
@param value     * Array that is the source of characters  源数组 * * @param offset * The initial offset 初始位 * * @param count * The length    复制的长度 * * @throws IndexOutOfBoundsException * If the {@code offset} and {@code count} arguments index * characters outside the bounds of the {@code value} array */ public String(char value[], int offset, int count) { if (offset < 0) {                        //开始位小于0抛出异常 throw new StringIndexOutOfBoundsException(offset); } if (count <= 0) {                        //复制长度小于等于0时 if (count < 0) {                      //复制长度小于0抛出异常 throw new StringIndexOutOfBoundsException(count); } if (offset <= value.length) {               //复制长度等于0且开始位置小于等于value长度时返回空的String this.value = "".value; return; } } // Note: offset or count might be near -1>>>1. if (offset > value.length - count) {             //开始位+长度大于源char数组的长度时抛出错误 throw new StringIndexOutOfBoundsException(offset + count); } this.value = Arrays.copyOfRange(value, offset, offset+count);  //调用Arrays.copyRange进行复制 }

 

4.public static char[] copyOfRange(char[] original, int from, int to)

Arrays的共有方法,按范围复制并返回一个新的char[]数组,从from位置开始(包括from),到to位置结束(不包括to)

char[] test=Array.copyOfRange(new char[]{'1','2','3'},1,2);

test------>23

    /**
     * Copies the specified range of the specified array into a new array.
     * The initial index of the range (<tt>from</tt>) must lie between zero
     * and <tt>original.length</tt>, inclusive.  The value at
     * <tt>original[from]</tt> is placed into the initial element of the copy
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
     * Values from subsequent elements in the original array are placed into
     * subsequent elements in the copy.  The final index of the range
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
     * may be greater than <tt>original.length</tt>, in which case
     * <tt>'\u000'</tt> is placed in all elements of the copy whose index is
     * greater than or equal to <tt>original.length - from</tt>.  The length
     * of the returned array will be <tt>to - from</tt>.
     * 从源char[]数组复制成新的char[]数组
   * *
@param original the array from which a range is to be copied      源char[]数组 * @param from the initial index of the range to be copied, inclusive   源数组的开始复制位置(包括在内) * @param to the final index of the range to be copied, exclusive.     源数组的复制结束位置(不包括在内) * (This index may lie outside the array.) * @return a new array containing the specified range from the original array, * truncated or padded with null characters to obtain the required length * @throws ArrayIndexOutOfBoundsException if {@code from < 0} * or {@code from > original.length} * @throws IllegalArgumentException if <tt>from &gt; to</tt> * @throws NullPointerException if <tt>original</tt> is null * @since 1.6 */ public static char[] copyOfRange(char[] original, int from, int to) { int newLength = to - from; if (newLength < 0)                            //新数组长度小于0抛出异常 throw new IllegalArgumentException(from + " > " + to); char[] copy = new char[newLength];                   //新建一个数组 System.arraycopy(original, from, copy, 0,               //调用arraycopy来复制修改新数组的值   Math.min(original.length - from, newLength)); return copy; }
原文地址:https://www.cnblogs.com/ming-szu/p/9063071.html