一文带你看穿String

1.1 前言

        String对象是不可变的。String类中每一个看起来会修改String值的方法,例如拼接、裁剪字符串,实际上都会创建一个全新的String对象,用来包含修改后的字符串内容。因此字符串的相关操作往往对性能有明显的影响。

1.2 定义

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence

        从代码可以看出String是final类型的,表示该类不能被继承,并且实现了Serializable、Comparable、CharSequence三个接口。

  • Serializable接口,表明String类是可序列化的。
  • Comparable接口,提供了一个compareTo(T o) 方法。
  • CharSequence接口,提供了length(),charAt(int index),subSequence(int start,int end),toString()方法。

1.3 属性

//final类型的字符数组,用于存储字符串内容
private final char value[];

//存放字符串的哈希值
private int hash; // Default to 0

//序列化id
private static final long serialVersionUID = -6849794470754667710L;

1.4 构造函数

//不含参数的构造函数
public String() {
    this.value = "".value;
}
//使用字符串类型的参数来初始化
public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}
//使用字符数组初始化
public String(char value[]) {
    this.value = Arrays.copyOf(value, value.length);//将原有的字符数组中的内容逐一的复制到String中的字符数组中
}
//从位置offset复制count个字符
public String(char value[], int offset, int count) {
    if (offset < 0) {
        throw new StringIndexOutOfBoundsException(offset);
    }
    if (count <= 0) {
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        if (offset <= value.length) {
            this.value = "".value;
            return;
        }
    }
    // 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);
}
//使用整型数组初始化
public String(int[] codePoints, int offset, int count) {
    if (offset < 0) {
        throw new StringIndexOutOfBoundsException(offset);
    }
    if (count <= 0) {
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        if (offset <= codePoints.length) {
            this.value = "".value;
            return;
        }
    }
    // Note: offset or count might be near -1>>>1.
    if (offset > codePoints.length - count) {
        throw new StringIndexOutOfBoundsException(offset + count);
    }

    final int end = offset + count;

    // Pass 1: Compute precise size of char[]
    int n = count;
    for (int i = offset; i < end; i++) {
        int c = codePoints[i];
        if (Character.isBmpCodePoint(c))
            continue;
        else if (Character.isValidCodePoint(c))
            n++;
        else throw new IllegalArgumentException(Integer.toString(c));
    }

    // Pass 2: Allocate and fill in char[]
    final char[] v = new char[n];

    for (int i = offset, j = 0; i < end; i++, j++) {
        int c = codePoints[i];
        if (Character.isBmpCodePoint(c))
            v[j] = (char)c;
        else
            Character.toSurrogates(c, v, j++);
    }

    this.value = v;
}
//检查字符数组是否越界
private static void checkBounds(byte[] bytes, int offset, int length) {
    if (length < 0)
        throw new StringIndexOutOfBoundsException(length);
    if (offset < 0)
        throw new StringIndexOutOfBoundsException(offset);
    if (offset > bytes.length - length)
        throw new StringIndexOutOfBoundsException(offset + length);
}
//从bytes数组中的offset位置开始,将长度为length的字符,使用charsetName格式解码,初始化字符串
public String(byte bytes[], int offset, int length, String charsetName)
        throws UnsupportedEncodingException {
    if (charsetName == null)
        throw new NullPointerException("charsetName");
    checkBounds(bytes, offset, length);
    this.value = StringCoding.decode(charsetName, bytes, offset, length);
}
//从bytes数组中的offset位置开始,将长度为length的字符,使用charset解码,初始化字符串
public String(byte bytes[], int offset, int length, Charset charset) {
    if (charset == null)
        throw new NullPointerException("charset");
    checkBounds(bytes, offset, length);
    this.value =  StringCoding.decode(charset, bytes, offset, length);
}
//通过charsetName来解码指定的byte数组,将其解码成unicode的char[]数组,够造成新的String
public String(byte bytes[], String charsetName)
        throws UnsupportedEncodingException {
    this(bytes, 0, bytes.length, charsetName);
}
//通过charset来解码指定的byte数组,将其解码成unicode的char[]数组,够造成新的String
public String(byte bytes[], Charset charset) {
    this(bytes, 0, bytes.length, charset);
}
//从bytes数组中的offset位置开始,将长度为length的字符,初始化字符串
public String(byte bytes[], int offset, int length) {
    checkBounds(bytes, offset, length);
    this.value = StringCoding.decode(bytes, offset, length);
}
//使用字节数组来初始化
public String(byte bytes[]) {
    this(bytes, 0, bytes.length);
}
//使用StringBuffer来构建字符串,不建议使用,可以使用StringBuffer.toString()来得到字符串
public String(StringBuffer buffer) {
    synchronized(buffer) {
        this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
    }
}
//使用StringBuilder来构建字符串,不建议使用,可以使用StringBuilder.toString()来得到字符串
public String(StringBuilder builder) {
    this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
//保护类型的构造函数,其中参数share没有被使用,加入这个share的只是为了区分于String(char[] value)方法,
//String(char[] value)方法在创建String的时候会用到 会用到Arrays的copyOf方法将value中的内容逐一复制到String当中,而这个String(char[] value, boolean share)方法则是直接将value的引用赋值给String的value。那么也就是说,这个方法构造出来的String和参数传过来的char[] value共享同一个数组。
//优点:性能好,节约内存
//该方法之所以设置为protected,是因为一旦该方法设置为公有,那就破坏了字符串的不可变性
String(char[] value, boolean share) {
    // assert share : "unshared not supported";
    this.value = value;
}

1.5 常用方法

1.5.1 length()

//返回字符串长度
public int length() {
    return value.length;
}

1.5.2 isEmpty()

//返回字符串是否为空
public boolean isEmpty() {
    return value.length == 0;
}

1.5.3 charAt(int index)

//返回字符串中第(index+1)个字符
public char charAt(int index) {
    if ((index < 0) || (index >= value.length)) {
        throw new StringIndexOutOfBoundsException(index);
    }
    return value[index];
}

1.5.4 startsWith(String prefix,int toffset)

//用于检测字符串是否以指定的前缀开始,其中toffset是字符串中开始查找的位置
public boolean startsWith(String prefix, int toffset) {
    char ta[] = value;
    int to = toffset;
    char pa[] = prefix.value;//字串
    int po = 0;
    int pc = prefix.value.length;//字串长度
    // Note: toffset might be near -1>>>1.
    if ((toffset < 0) || (toffset > value.length - pc)) {//如果toffset为负或大于此String对象的长度,返回false
        return false;
    }
    while (--pc >= 0) {
        if (ta[to++] != pa[po++]) {//依次比较
            return false;
        }
    }
    return true;
}

1.5.5 endsWith(String suffix)

//此字符串是否以指定的后缀结束
public boolean endsWith(String suffix) {
    return startsWith(suffix, value.length - suffix.value.length);
}

1.5.6 index(int ch,int fromIndex)

//返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索,如果此字符串中没有这样的字符,则返回 -1
public int indexOf(int ch, int fromIndex) {
    final int max = value.length;
    if (fromIndex < 0) {
        fromIndex = 0;
    } else if (fromIndex >= max) {
        // Note: fromIndex might be near -1>>>1.
        return -1;
    }

    if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
        // handle most cases here (ch is a BMP code point or a
        // negative value (invalid code point))
        final char[] value = this.value;
        for (int i = fromIndex; i < max; i++) {
            if (value[i] == ch) {
                return i;
            }
        }
        return -1;
    } else {
        return indexOfSupplementary(ch, fromIndex);
    }
}

1.5.7 substring(int beginIndex)

//返回一个新的字符串,它是此字符串的一个子字符串
//使用String(value, beginIndex, subLen)方法创建一个新的String并返回,这个方法会将原来的char[]中的值逐一复制到新的String中
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);
}

1.5.8 concat(String str)

//拼接字符串
public String concat(String str) {
    int otherLen = str.length();
    if (otherLen == 0) {
        return this;
    }
    int len = value.length;
    char buf[] = Arrays.copyOf(value, len + otherLen);
    str.getChars(buf, len);
    return new String(buf, true);
}

        concat(String str)方法首先获取拼接字符串的长度,判断这个字符串长度是否为0(判断这个用来拼接的字符串是不是空串),如果是就返回原来的字符串(等于没有拼接);否则就获取源字符串的长度,创建一个新的char[]字符数组,这个字符数组的长度是拼接字符串的长度与源字符串的长度之和,通过Arrays类的copyOf方法复制源数组,然后通过getChars方法将拼接字符串拼接到源字符串中,然后将新串返回。

1.5.9 replace(char oldChar,char newChar)

//将字符串中的oldChar 字符换成 newChar 字符
public String replace(char oldChar, char newChar) {
    if (oldChar != newChar) {
        int len = value.length;
        int i = -1;
        char[] val = value; /* avoid getfield opcode */

        while (++i < len) {//先找到旧值最开始出现的位置,减少对比的时间,有效提升效率
            if (val[i] == oldChar) {
                break;
            }
        }
        //从找到旧值那个位置开始,直到末尾,用新值代替出现的旧值
        if (i < len) {
            char buf[] = new char[len];
            for (int j = 0; j < i; j++) {
                buf[j] = val[j];
            }
            while (i < len) {
                char c = val[i];
                buf[i] = (c == oldChar) ? newChar : c;
                i++;
            }
            return new String(buf, true);
        }
    }
    return this;
}

1.5.10 contains(CharSequence s)

//判断字符串是否包含字符序列 s
public boolean contains(CharSequence s) {
    return indexOf(s.toString()) > -1;
}

1.5.11 trim()

//去掉字符串两端空格
public String trim() {
    int len = value.length;
    int st = 0;
    char[] val = value;    /* avoid getfield opcode */
    //找到字符串前端没有空格的位置
    while ((st < len) && (val[st] <= ' ')) {
        st++;
    }
    //找到字符串末尾没有空格的位置
    while ((st < len) && (val[len - 1] <= ' ')) {
        len--;
    }
    //如果前后都没有出现空格,返回字符串本身
    return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}

1.5.12  toCharArray()

//将字符串转化成字符数组
public char[] toCharArray() {
    // Cannot use Arrays.copyOf because of class initialization order issues
    char result[] = new char[value.length];
    System.arraycopy(value, 0, result, 0, value.length);
    return result;
}

1.5.13 equals(Object anObject)

//比较对象
public boolean equals(Object anObject) {
    if (this == anObject) {//判断当前对象与anObject是不是同一个对象,若是,直接返回true
        return true;
    }
    if (anObject instanceof String) {//anObject是不是String类型的,如果不是,直接返回false
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {//比较两个数组长度是否相等,若不相等,返回false
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {//循环逐一比较值,若都相等者返回true
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

1.5.14 compareTo(String anotherString)

//比较字符串
public int compareTo(String anotherString) {
    int len1 = value.length;
    int len2 = anotherString.value.length;
    int lim = Math.min(len1, len2);//取两个字符串的长度的最小值
    char v1[] = value;
    char v2[] = anotherString.value;

    int k = 0;
    while (k < lim) {
        char c1 = v1[k];
        char c2 = v2[k];
        if (c1 != c2) {
            return c1 - c2;
        }
        k++;
    }
    return len1 - len2;
}
  • 判断两个字符串的长度是否相等。
  • 若相等,再继续判断每个字符是否相同,若相同则返回0,不相同,则返回第一个不同字符的ascii码的差值。
  • 若不相等,则判断短的字符串是否是长串的字串,若是,则返回长度的差值,若不是,则返回第一个不同字符的ascii码的差值。

1.6 方法总结

方法名 说明
length()  返回字符串长度
isEmpty() 返回字符串是否为空
charAt(int index) 返回字符串中第(index+1)个字符
char[] toCharArray() 转化成字符数组
trim() 去掉字符串两端空格
toUpperCase() 转化为大写
toLowerCase() 转化为小写
concat(String str) 拼接字符串
replace(char oldChar, char newChar) 将字符串中的
oldChar 字符换成 newChar 字符
boolean matches(String regex) 判断字符串是否匹配给定的regex正则表达式
boolean contains(CharSequence s) 判断字符串是否包含字符序列 s
String[] split(String regex, int limit) 按照字符 regex将字符串分成 limit 份
String[] split(String regex) 按照字符 regex 将字符串分段
equals(Object anObject) 比较对象
equalsIgnoreCase(String anotherString) 忽略大小写比较字符串对象
startsWith(String prefix,int toffset) 字符串从指定索引开始的子字符串是否以指定前缀开始
endsWith(String suffix) 此字符串是否以指定的后缀结束

 

1.7 String的一些注意点

  • String 对 “+” 的支持其实就是使用了 StringBuilder 以及他的 append、toString 两个方法。
  • 字符串的 switch 是通过 equals() 和 hashCode() 方法来实现的。记住,switch 中只能使用整型,比如 byte,short,char(ackii码是整型) 以及 int。

1.8 String经典的面试题

String s1="abc"; 
String s2="abc"; 
System.out.println(s1==s2); 
System.out.println(s1.equals(s2));
/*output: 
true true 
*/

    该题主要考察对于java常量池的理解,先在常量池中创建”abc“,并指向s1,而后在创建s2时,由于常量池中已经存在”abc“,只需指向s2就可以,而不需要再创建。”==”在这里比较的是对象引用,故结果为”true”,String 中的equals方法经过重写后操作为比较此字符串与指定的对象的值是否相等,因此是true。

String s1=new String("abc");
String s2="abc";
System.out.println(s1==s2); 
System.out.println(s1.equals(s2));
/*output:
false
true
*/

    s1是通过new创建的对象在堆内存,s2在方法区中的常量池中,因此地址不一样,==是false。

String s1="a"+"b"+"c";
String s2="abc";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
/*output:
true
true
*/

    编译时s1已经成为“abc”在常量池中查找创建,s2不需要再创建。

String s1="ab";
String s2="abc";
String s3=s1+"c";
System.out.println(s3==s2);
System.out.println(s3.equals(s2));
/*output:
false
true
*/

    先在常量池中创建”ab“,地址指向s1,再创建”abc”,指向s2。对于s3,先创建StringBuilder(或 StringBuffer)对象,通过append连接得到“abc”,再调用toString()转换得到的地址指向s3。故(s3==s2)为false。

1.9总结

  • 一旦 String 对象在内存(堆)中被创建出来,就无法被修改。
  • 如果你需要一个可修改的字符串,应该使用 StringBuffer 或者
    StringBuilder。
  • 如果你只需要创建一个字符串,你可以使用双引号的方式,如果你需要在堆中创建一个新的对象,你可以选择构造函数的方式。

参考资料

https://www.jianshu.com/p/799c4459b808

https://blog.csdn.net/Sqirt/article/details/72765071

原文:https://my.oschina.net/wuchanghao/blog/1831272

猜你喜欢

转载自blog.csdn.net/u011418530/article/details/80743332
今日推荐