二 、在 JDK 6 and JDK 7中 substring() 方法

在JDK6 和JDK 7 里面substring(int beginIndex, int endIndex)的方法是不同的。知道这种区别会帮助你更好用它们。为了简单期间,下面用substring() 来表示 substring(int beginIndex,Int endIndex) 方法。

1、 substring()方法做什么
substring(int beginIndex,int endIndex)方法返回一个从beginIndex开始,到endIndex-1结束的字符串。

String x = "abcdef";
x = x.substring(1,3);
System.out.println(x);

输出:
bc

2、 当subString()方法被调用时候发生了什么?
你也许知道,因为x是不可变的,所以当x被赋值为x.substring(1,3)的时候,它指向了一个全新的字符串,如下图所示:
string-substring1


但是,这个图片没有正确的说明在堆里面发生的事情。在JDK6和JDK7 中调用substring()真正发生了什么。

3、 在JDK6中substring()
String 可以被标示成字符数组。在JDK6中,String 类包含三个属性:char value[], int offset,int count。它们被分别用于存储实际的字符串数组,数组第一个元素的标示,字符串中字符的个数。
当substring()方法被调用时候,它创建了一个新的字符串,但是这个字符串的值仍然指向在堆中相同的数组。原来字符串和新字符串的区别是他们的count和offset的值不同。
string-substring2

下面的代码是简化的,关于这个问题的关键解释。
//JDK 6

String(int offset, int count, char value[]) {
    this.value = value;
    this.offset = offset;
    this.count = count;
}
 
public String substring(int beginIndex, int endIndex) {
    //check boundary
    return  new String(offset + beginIndex, endIndex - beginIndex, value);
}


4、 在JDK6中substring()引起的问题
如果你有个非常长的字符串,但是你每次想通过用substring()来取其中一小部分。这将会产生一个性能问题,你仅仅需要一小部分,但是你保留了整个。在JDK6中,通过下面的方法,将使它指向一个真正的子串。
x = x.substring(x,y)+””
注意:推荐x = new String(x.substring(x, y)); 这样解决这个问题更好些。
5、 在JDK7中substring()
这些在JDK7中这些得到了改进。在JDK7中,substring()方法时间上在堆上创建了一个新的数组。
string-substring3

//JDK 7
public String(char value[], int offset, int count) {
    //check boundary
    this.value = Arrays.copyOfRange(value, offset, offset + count);
}
 
public String substring(int beginIndex, int endIndex) {
    //check boundary
    int subLen = endIndex - beginIndex;
    return new String(value, beginIndex, subLen);
}
原文地址:https://www.cnblogs.com/seaspring/p/5268254.html