String之substring()

                                      String之substring()


示例代码:

    String a = new String("AAA");

   a = a.substring(1);

   System.out.println(a);


剖析一:    String a = new String("AAA");

    

 1.new一个字符串队先后,我们进入 这个方法

       public String(String original) {

        this.value = original.value;

        this.hash = original.hash;

    }

    original: 我们自己定义的字符串

    this.value: 把我们输出的字符串赋值给String类中的 this.value    

/** The value is used for character storage. */

    private final char value[];

            可以得知,value是String 类中用来存储字符串的一个 字符数组.并且是final 类型,代表是不可以修改的.

            由此,我们可知新建一个字符串,java是用 "字符数组" 来存储的.    

    this.hash: 这个是hash值,我们暂且不用管.

    

    2.我们继续向下看,得知String a,a接收到字符串"AAA"的地址,也就是它的引用.

    

    3.然后我们接着看   a = a.substring(1);

       public String substring(int beginIndex) {

        if (beginIndex < 0) {

            throw new StringIndexOutOfBoundsException(beginIndex);

        }

        int subLen = value.length - beginIndex;

        if (subLen < 0) {

            throw new StringIndexOutOfBoundsException(subLen);

        }

        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);

    }

    

      if (beginIndex < 0) { }是判断异常的 ,不用管

      int subLen 这个是我们输入的字符串(存入到的字符数组)的长度 - 我们要传进来的截取索引

      if (subLen < 0) { } 这个同样也是判断异常的,不用管  

      

      重要的是这句话:return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);

            1). if else 缩略形式 

            2). this代表当前类对象

            3). 如果beginIndex == 0 代表从第一个字符开始截取(相当于没截取).就返回当前字符串

              4). 否则,进入到新的方法 new String(value, beginIndex, subLen),继续跟随

            

    3). 下一个方法

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

        if (offset < 0) {

            throw new StringIndexOutOfBoundsException(offset);

        }

        if (count < 0) {

            throw new StringIndexOutOfBoundsException(count);

        }

        // Note: offset or count might be near -1>>>1.

        if (offset > value.length - count) {

            throw new StringIndexOutOfBoundsException(offset + count);

        }

        this.value = Arrays.copyOfRange(value, offset, offset+count);

    }

        

        可以得知,这个方法也并未实现了真正的截取,只是调用了Arrays.copyOfRange(value, offset, offset+count) 方法

        继续向下看

    

    4).Arrays类下的copyOfRange 方法

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

        int newLength = to - from;

        if (newLength < 0)

            throw new IllegalArgumentException(from + " > " + to);

        char[] copy = new char[newLength];

        System.arraycopy(original, from, copy, 0,

                         Math.min(original.length - from, newLength));

        return copy;

    }

    由方法名可知,这是一个复制相关的方法,此方法中,先生成了一个字符数组copy,调用System 类下的arraycopy方法进行复制

    5).System类下的方法

        public static native void arraycopy(Object src,  int  srcPos,

                                            Object dest, int destPos,

                                            int length);

        很明显,此方法也并未实现真正的截取,而是把具体操作交给了 JNI 接口中的 操作系统本地方法来实现,

        很遗憾,我们并没有找到 其真正的内功,但我们大概得知,此方法是将我们要截取的字符串复制的一个新的字符数组里.

原创文章 63 获赞 48 访问量 8万+

猜你喜欢

转载自blog.csdn.net/csp_6666/article/details/89158967
今日推荐