subString源码分析

Posted ming_szu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了subString源码分析相关的知识,希望对你有一定的参考价值。

1. Public String subString(int beginIndex)

    /**返回目标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)

/**
     * 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)

/**
     * 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)

    /**
     * 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; }

 

以上是关于subString源码分析的主要内容,如果未能解决你的问题,请参考以下文章

Android 事件分发事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )(代码片段

subString源码分析

go源码分析:strings包

《Docker 源码分析》全球首发啦!

mysql jdbc源码分析片段 和 Tomcat's JDBC Pool

Android 逆向ART 脱壳 ( DexClassLoader 脱壳 | DexClassLoader 构造函数 | 参考 Dalvik 的 DexClassLoader 类加载流程 )(代码片段