String

目录(本文基于JDK1.8)


 

 

1.String类的定义

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

String 类是java.lang 包下的一个类,我们在日常工作中用到它的地方很多,现在我们就一起学习一下。

  • String类是final修饰的,这就代表String类是不能被继承的。
  • 实现Serializable接口代表String是可以序列化的。
  • 实现Comparable接口通过compareTo方法按顺序比较单个字符的ASCII码,来比较两个字符串的大小。
  • 实现CharSequence接口表示是一个有序字符的集合。

2.字段属性

    /** The value is used for character storage. */
    private final char value[];

    /** Cache the hash code for the string */
    private int hash; // Default to 0

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -6849794470754667710L;

    /**
     * Class String is special cased within the Serialization Stream Protocol.
     *
     * A String instance is written into an ObjectOutputStream according to
     * <a href="{@docRoot}/../platform/serialization/spec/output.html">
     * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
     */
    private static final ObjectStreamField[] serialPersistentFields =
        new ObjectStreamField[0];
  • value[]

    我们可以看到String是一个字符数组,并且经过final修饰的变量一经初始化便无法再修改,因此说String是不可变的。

  • hash

    缓存字符串的hash Code值,默认值为0。

  • serialVersionUID

    实现了io流的Serializable接口,所以支持序列化和反序列化支持。Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常(InvalidCastException).

  • serialPersistentFields

ObjectStreamField是Java.io包下面的一个类,ObjectStreamFields数组用来声明一个类的序列化字段,在这里这个类主要用来提取序列化过程中某个对象内的字段【成员属性】元数据信息,包括字段名、字段类型、JVM签名、反射对象、实例数据的偏移量、是否通过ObjectStreamField实例所表示的序列化字段是独享的等。


3.构造方法

3.1 无参构造方法

  /**
     * 初始化创建一个新的String对象用来表示一个空字符串
     */
    public String() {
        this.value = "".value;
    }

3.2 String构造方法

    /**
     * 通过传入一个字符串参数来构建一个空的String对象,换句话说,新创建的字符串对象是 
     * 传入的字符串参数的一个副本。除非你确实需要显式的复制一个字符串对象,否则你完全 
     * 没必要使用此构造器来创建一个String对象,因为String自身已经被设计为不可变。
     *
     * @param  original 原始的字符串对象
     */
    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

3.3 字符数组构造方法

  /**
     * 通过传入的一个字符数组来构建一个空的String对象,新创建的String对象是传入的字符数组的 
     * 一个副本,后续你对该字符数组对象的修改不会影响到当前新创建的String对象 
     * @param  value  字符数组 
     */
    public String(char value[]) {
        this.value = Arrays.copyOf(value, value.length);
    }


    /** 
     * 通过传入的一个字符数组并根据指定的offset和count参数来截取得到一个子字符数组, 
     * 然后根据子字符数组来构建一个新的字符串对象,新创建的字符串对象是子字符数组内容的 
     * 一个副本,后续你对该子字符数组内容的修改不会影响到当前新创建的字符串对象。其中offset 
     * 参数表示截取子字符数组时在第一个参数即原字符数组中的起始偏移量,count表示子字符数组的长度。 
     * @param  value   原字符数组 
     * @param  offset  截取子字符数组时在原字符数组中的起始偏移量 
     * @param  count   子字符数组的长度 
     * 
     * @throws  IndexOutOfBoundsException  如果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);
    }
    
    /** 
     * 通过传入的一个代码点数组并根据指定的offset和count参数来截取得到一个子字符数组, 
     * 然后根据子字符数组来构建一个新的字符串对象,新创建的字符串对象是子字符数组内容的 
     * 一个副本,后续你对该代码点数组内容的修改不会影响到当前新创建的字符串对象。其中offset 
     * 参数表示在原代码点数组中截取的起始偏移量,count表示在原代码点数组中截取的元素长度。 
     * 代码点最终会被转换成字符 
     * @param  codePoints    原代码点数组 
     * @param  offset       在原代码点数组中截取的起始偏移量 
     * @param  count       在原代码点数组中截取的元素长度 
     * 
     * @throws  IndexOutOfBoundsException  如果offset或count参数越界了,那么会抛出此异常 
     * @since  1.5 
     */  
    public String(int[] codePoints, int offset, int count) {  
        if (offset < 0) {  
            throw new StringIndexOutOfBoundsException(offset);  
        }  
        if (count < 0) {  
            throw new StringIndexOutOfBoundsException(count);  
        }  
        // 如果offset + count > value.length,则会抛出字符串越界异常  
        if (offset > codePoints.length - count) {  
            throw new StringIndexOutOfBoundsException(offset + count);  
        }  
  
        final int end = offset + count;  
  
        // 阶段1:计算char[]的精确大小  
        int n = count;  
        for (int i = offset; i < end; i++) {  
            int c = codePoints[i];  
            //判断一个代码点是否在基本多语言面(Basic Multilingual Plane,BMP)内  
            if (Character.isBmpCodePoint(c)) {  
                continue;  
            }  
            //判断是否为一个合法的代码点  
            else if (Character.isValidCodePoint(c)) {  
                n++;  
            }  
            else {  
                throw new IllegalArgumentException(Integer.toString(c));  
            }  
        }  
  
        // 阶段2:收集代码点、转换成字符char,并装入char[]  
        final char[] v = new char[n];  
        for (int i = offset, j = 0; i < end; i++, j++) {  
            int c = codePoints[i];  
            //判断一个代码点是否在基本多语言面(Basic Multilingual Plane,BMP)内  
            if (Character.isBmpCodePoint(c)) {  
                // 将代码点转换成一个字符,BMP Code Point代码点是65535是2的16次方,  
                // 刚好是两个字节(即一个字)的大小。在超出两个字节后只能算是有效的代码点,  
                // 并非是BMP Code Point代码点。从代码中也可看出,BmpCodePoint代码点的整数是  
                // 可以直接强转成char类型的。在java中char类型刚好占2个字节,在2个字节以内的整数  
                // 都可以直接强转换成char类型  
                v[j] = (char) c;  
            }  
            else {  
                /** 
                 * Surrogate这个概念,不是来自Java语言,而是来自Unicode编码方式之一:UTF-16。 
                 * 具体请见: <a href="https://en.wikipedia.org/wiki/UTF-16">UTF-16</a> 
                 * 简而言之,Java语言內部的字符信息是使用UTF-16编码。因为char这个类型是16-bit的, 
                 * 它可以有65536种取值,即65536个编号,每个编号可以代表1种字符。但是Unicode 
                 * 包含的字符已经远远超过65536个。那编号大于65536的,还要用16-bit编码,该怎么办? 
                 * 于是Unicode标准制定组想出的办法就是,从这65536个编号里,拿出2048个,规定它们是「Surrogates」, 
                 * 让它们两个为一组,来代表编号大于65536的那些字符。更具体地, 
                 * 编号从U+D800至U+DBFF的规定为「High Surrogates」,共1024个。 
                 * 编号为 U+DC00至U+DFFF的规定为「Low Surrogates」,也是1024个, 
                 * 它们两两组合出现,就又可以多表示1048576种字符。 
                 */  
                Character.toSurrogates(c, v, j++);  
            }  
        }  
        this.value = v;  
    }  

String就是使用字符数组(char[])实现的。所以我们可以使用一个字符数组来创建一个String,那么这里值得注意的是,当我们使用字符数组创建String的时候,会用到Arrays.copyOf方法和Arrays.copyOfRange。这两个方法是将原有的字符数组中的内容逐一的复制到String中的字符数组中。同样,我们也可以用一个String类型的对象来初始化一个String。这里将直接将源String中的valuehash两个属性直接赋值给目标String。因为String一旦定义之后是不可以改变的,所以也就不用担心改变源String的值会影响到目标String的值。

当然,在使用字符数组来创建一个新的String对象的时候,不仅可以使用整个字符数组,也可以使用字符数组的一部分,只要多传入两个参数int offsetint count就可以了。

3.4 字节数组构造方法

   /** 
     * 通过传入一个ASCII码的字节数组来构建一个新的字符串对象 
     * 注意:此方法不能正确的将字节数组转成字符,自JDK1.1版本起,实现此功能更佳的方式是 
     * 使用带charset(字符编码)参数的构造器来构建字符串对象,或者使用系统平台默认的字符集编码 
     * 
     * @param  ascii    用来转换成字符串的ASCII码的字节数组 
     * @param  hibyte  每16位的Unicode编码单元的前8位 
     * @param  offset  截取ASCII码的字节数组的起始偏移量 
     * @param  count   在ASCII码的字节数组中截取的元素长度 
     * 
     * @throws  IndexOutOfBoundsException  如果offset或count参数越界了,那么会抛出此异常 
     * 
     * @see  #String(byte[], int) 
     * @see  #String(byte[], int, int, java.lang.String) 
     * @see  #String(byte[], int, int, java.nio.charset.Charset) 
     * @see  #String(byte[], int, int) 
     * @see  #String(byte[], java.lang.String) 
     * @see  #String(byte[], java.nio.charset.Charset) 
     * @see  #String(byte[]) 
     */  
    @Deprecated  
    public String(byte ascii[], int hibyte, int offset, int count) {  
        checkBounds(ascii, offset, count);  
        char value[] = new char[count];  
        //高8位如果是零,说明是正数  
        if (hibyte == 0) {  
            for (int i = count; i-- > 0;) {  
                //因为byte的取值范围是 -128~127,而Char是0~65535  
                //所以需要将前8位全部清为零,从而使得byte原来的负值变成正值  
                //0xff的二进制为1111 1111  
                value[i] = (char)(ascii[i + offset] & 0xff);  
            }  
        } else {  
            // <<=带符号的左移动运算,向左移动8位,则最右边的8位全部清零  
            hibyte <<= 8;  
            for (int i = count; i-- > 0;) {  
                value[i] = (char)(hibyte | (ascii[i + offset] & 0xff));  
            }  
        }  
        this.value = value;  
    }  

   /** 
     * 通过传入一个ASCII码的字节数组来构建一个新的字符串对象, 
     * 直接从offset偏移量位置截取到字节数组的末尾 
     * @param ascii     用来转换成字符串的ASCII码的字节数组 
     * @param hibyte    每16位的Unicode编码单元的前8位 
     */  
    @Deprecated  
    public String(byte ascii[], int hibyte) {  
        this(ascii, hibyte, 0, ascii.length);  
    }  
  
    /** 
     * 检查offset,count参数在字节数组中是否越界的工具方法 
     * @param bytes     目标字节数组 
     * @param offset    起始偏移量 
     * @param length    截取长度 
     */  
    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);  
    }  
  
    /** 
     * 通过StringCoding类的decode方法将指定的字节数组按照指定的字符集编码 
     * 解码为一个字符串对象 
     * @param bytes         字节数组 
     * @param offset        截取起始偏移量 
     * @param length        截取的长度 
     * @param charsetName   字符集编码 
     * @throws UnsupportedEncodingException 
     */  
    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);  
    }  
  
    /** 
     * 通过StringCoding类的decode方法将指定的字节数组按照指定的字符集编码 
     * 解码为一个字符串对象 
     * @param bytes         字节数组 
     * @param offset        截取起始偏移量 
     * @param length        截取的长度 
     * @param 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);  
    }  
  
    /** 
     * 通过StringCoding类的decode方法将指定的字节数组按照指定的字符集编码 
     * 解码为一个字符串对象(重载) 
     * 
     * @since  JDK1.1 
     */  
    public String(byte bytes[], String charsetName)  
            throws UnsupportedEncodingException {  
        this(bytes, 0, bytes.length, charsetName);  
    }  
  
    /** 
     * 通过StringCoding类的decode方法将指定的字节数组按照指定的字符集编码 
     * 解码为一个字符串对象(重载) 
     * @since  1.6 
     */  
    public String(byte bytes[], Charset charset) {  
        this(bytes, 0, bytes.length, charset);  
    }  
  
    /** 
     * 通过StringCoding类的decode方法将指定的字节数组按照系统平台 
     * 的默认字符集编码解码为一个字符串对象(重载) 
     * @since  1.6 
     */  
    public String(byte bytes[], int offset, int length) {  
        checkBounds(bytes, offset, length);  
        this.value = StringCoding.decode(bytes, offset, length);  
    }  
  
    /** 
     * 通过StringCoding类的decode方法将指定的字节数组按照系统平台 
     * 的默认字符集编码解码为一个字符串对象(重载) 
     * 这里没有指定offset,count参数,则默认会直接截取[0,length() -1]范围内的字节即 
     * 默认会直接将整个字节数组解码为一个新的字符串对象 
     * @since  1.6 
     */  
    public String(byte bytes[]) {  
        this(bytes, 0, bytes.length);  
    }  
  
  
    /** 
     * 根据传入的StringBuffer对象构造一个新的String对象,内部会将StringBuffer对象 
     * 内的字符数组都复制到当前对象的value属性上,注意此方法是加锁的即线程安全的。 
     * @param buffer 
     */  
    public String(StringBuffer buffer) {  
        synchronized(buffer) {  
            this.value = Arrays.copyOf(buffer.getValue(), buffer.length());  
        }  
    }  

在Java中,String实例中保存有一个char[]字符数组,char[]字符数组是以unicode码来存储的,String 和 char 为内存形式,byte是网络传输或存储的序列化形式。所以在很多传输和存储的过程中需要将byte[]数组和String进行相互转化。所以,String提供了一系列重载的构造方法来将一个字符数组转化成String,提到byte[]和String之间的相互转换就不得不关注编码问题。String(byte[] bytes, Charset charset)是指通过charset来解码指定的byte数组,将其解码成unicode的char[]数组,够造成新的String

这里的bytes字节流是使用charset进行编码的,想要将他转换成unicode的char[]数组,而又保证不出现乱码,那就要指定其解码方式

如果我们在使用byte[]构造String的时候,使用的是带有charsetName或者charset参数的一种的话,那么就会使用StringCoding.decode方法进行解码,使用的解码的字符集就是我们指定的charsetName或者charset。 我们在使用byte[]构造String的时候,如果没有指明解码使用的字符集的话,那么StringCodingdecode方法首先调用系统的默认编码格式,如果没有指定编码格式则默认使用ISO-8859-1编码格式进行编码操作。主要体现代码如下:

 static char[] decode(byte[] ba, int off, int len) {
        String csn = Charset.defaultCharset().name();
        try {
            // use charset name decode() variant which provides caching.
            return decode(csn, ba, off, len);
        } catch (UnsupportedEncodingException x) {
            warnUnsupportedCharset(csn);
        }
        try {
            return decode("ISO-8859-1", ba, off, len);
        } catch (UnsupportedEncodingException x) {
            // If this code is hit during VM initialization, MessageUtils is
            // the only way we will be able to get any kind of error message.
            MessageUtils.err("ISO-8859-1 charset not available: "
                             + x.toString());
            // If we can not find ISO-8859-1 (a required encoding) then things
            // are seriously wrong with the installation.
            System.exit(1);
            return null;
        }
    }

3.5 StringBuffer和StringBuider构造

    /** 
     * 根据传入的StringBuffer对象构造一个新的String对象,内部会将StringBuffer对象 
     * 内的字符数组都复制到当前对象的value属性上,注意此方法是加锁的即线程安全的。 
     * @param buffer 
     */  
    public String(StringBuffer buffer) {  
        synchronized(buffer) {  
            this.value = Arrays.copyOf(buffer.getValue(), buffer.length());  
        }  
    }  
  
    /** 
     * 根据传入的StringBuilder对象构造一个新的String对象,内部会将StringBuilder对象 
     * 内的字符数组都复制到当前对象的value属性上,注意此方法不是线程安全的。 
     * @since  1.5 
     */  
    public String(StringBuilder builder) {  
        this.value = Arrays.copyOf(builder.getValue(), builder.length());  
    }  

这两个构造方法很少用到,因为有了StringBufferStringBuilder对象后,可以直接toString()方法获取String。

3.6 一个特殊的保护类型的构造方法

    /*
    * 包私有构造器,它能快速的为数组赋值.
    * 目前不支持使用false,只使用true.
    */
    String(char[] value, boolean share) {
        // assert share : "unshared not supported";
        this.value = value;
    }

我们看一下它和String(char[] value)的区别:

  • 第一个区别是该方法多了一个参数:boolean share,其实这个参数在方法体中根本没被使用,参照注释,可以知道,加入这个share的只是为了区分于String(char[] value)方法,不加这个参数就没办法定义这个函数,只有参数不同才能进行重载
  • 第二个区别就是具体的方法实现不同。我们前面提到过,String(char[] value)方法在创建String的时候会用到 会用到ArrayscopyOf方法将value中的内容逐一复制到String当中,而这String(char[] value, boolean share)个方法则是直接将value的引用赋值给String的value。那么也就是说,这个方法构造出来的String和参数传过来的char[] value共享同一个数组。那么为什么Java会提供这样一个方法呢?
  • 首先,性能好,一个是直接给数组赋值(相当于直接将String的value的指针指向char[]数组),一个是逐一拷贝。当然是直接赋值快了。
  • 其次,共享内部数组节约内存

该方法之所以设置为protected,是因为一旦该方法设置为公有,在外面可以访问的话,那就破坏了字符串的不可变性。例如如下YY情形:

    char[] arr = new char[] {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
    String s = new String(0, arr.length, arr); // "hello world"
    arr[0] = 'a'; // replace the first character with 'a'
    System.out.println(s); // aello world

如果构造方法没有对arr进行拷贝,那么其他人就可以在字符串外部修改该数组,由于它们引用的是同一个数组,因此对arr的修改就相当于修改了字符串。

所以,从安全性角度考虑,他也是安全的。对于调用他的方法来说,由于无论是原字符串还是新字符串,其value数组本身都是String对象的私有属性,从外部是无法访问的,因此对两个字符串来说都很安全。

4.其他方法

4.1 常用方法

    /** 
     * 返回一个字符串的长度,此长度必须等于Unicode编码单元的长度。 
     * 一个Unicode编码单元为16个bit,而一个char字符占2个字节刚好16个bit,即 
     * 一个字符串的长度等于它包含的字符个数。 
     * 
     * @return  返回当前字符串的长度 
     */  
    public int length() {  
        return value.length;  
    }  
  
    /** 
     * 判断一个字符串是否为一个空字符串[""] 
     * 当且仅当字符串的length()等于零,此方法才返回true,否则返回false 
     * 
     * @since 1.6 
     */  
    public boolean isEmpty() {  
        return value.length == 0;  
    }  
  
    /** 
     * 返回字符串某个索引位置的字符 
     * 
     * @param      index   字符的索引位置,从零开始计算 
     * @return     返回指定索引位置的字符 
     * @exception  IndexOutOfBoundsException 
     *             若index参数为负数或者index参数不小于length(),则会抛出索引越界异常 
     */  
    public char charAt(int index) {  
        if ((index < 0) || (index >= value.length)) {  
            throw new StringIndexOutOfBoundsException(index);  
        }  
        return value[index];  
    }  
  
    /** 
     * 返回字符串某个索引位置的Unicode code point(Unicode代码点) 
     * 
     * @param      index   字符的索引位置,从零开始计算 
     * @return     返回指定索引位置的Unicode code point(Unicode代码点) 
     * @exception  IndexOutOfBoundsException 
     *             若index参数为负数或者index参数不小于length(),则会抛出索引越界异常 
     * @since      1.5 
     */  
    public int codePointAt(int index) {  
        if ((index < 0) || (index >= value.length)) {  
            throw new StringIndexOutOfBoundsException(index);  
        }  
        return Character.codePointAtImpl(value, index, value.length);  
    }  
  
    /** 
     * 返回字符串某个索引位置之前的Unicode code point(Unicode代码点) 
     * 
     * @param      index   字符的索引位置,从零开始计算 
     * @return     返回指定索引位置之前的Unicode code point(Unicode代码点) 
     * @exception  IndexOutOfBoundsException 
     *             若index参数小于1或者大于length(),则会抛出索引越界异常 
     * @since      1.5 
     */  
    public int codePointBefore(int index) {  
        int i = index - 1;  
        if ((i < 0) || (i >= value.length)) {  
            throw new StringIndexOutOfBoundsException(index);  
        }  
        return Character.codePointBeforeImpl(value, index, 0);  
    }  
  
    /** 
     * 返回字符串的指定区间内的Unicode code point(Unicode代码点)的总个数 
     * 
     * @param beginIndex   起始索引位置 
     * @param endIndex    结束索引位置 
     * @return            返回指定区间范围内的Unicode代码点的总个数 
     * @exception IndexOutOfBoundsException 
     *            若beginIndex参数为负数或endIndex参数大于length(), 
     *            或者beginIndex参数大于endIndex参数,则会抛出索引越界异常 
     * @since  1.5 
     */  
    public int codePointCount(int beginIndex, int endIndex) {  
        if (beginIndex < 0 || endIndex > value.length || beginIndex > endIndex) {  
            throw new IndexOutOfBoundsException();  
        }  
        return Character.codePointCountImpl(value, beginIndex, endIndex - beginIndex);  
    }  
    
    /** 
     * 将当前字符串对象转换成一个字符数组 
     * 
     * @return  返回当前字符串对象转换后得到的一个字符数组,该字符数组的长度与 
     *          当前字符串的长度一致 
     */  
    public char[] toCharArray() {  
        // 由于类初始化问题,这里不能使用Arrays.copyOf()方法来实现  
        char result[] = new char[value.length];  
        System.arraycopy(value, 0, result, 0, value.length);  
        return result;  
    }  
    
    /** 
     * 剔除掉当前字符串对象的两头连续的空格字符,并返回一个新的字符串对象 
     * 
     * @return  返回剔除掉两头空格后的字符串对象 
     */  
    public String trim() {  
        int len = value.length;  
        int st = 0;  
        char[] val = value;  
  
        //从开头开始找空格字符  
        while ((st < len) && (val[st] <= ' ')) {  
            st++;  
        }  
        //从末尾往开头找空格字符  
        while ((st < len) && (val[len - 1] <= ' ')) {  
            len--;  
        }  
        //若没找到空格字符,则直接返回当前字符串对象,否则截取字符串并返回  
        return ((st > 0) || (len < value.length)) ? substring(st, len) : this;  
    }  
    
    /** 
     * 将指定的字符串拼接到当前字符串的末尾 
     * 若str参数表示的字符串的长度==0,那么直接返回this,否则会创建一个新的字符串对象,比如: 
     * "cares".concat("s") returns "caress" 
     * "to".concat("get").concat("her") returns "together" 
     * 
     * @param   str   需要拼接到当前字符串末尾的字符串对象 
     * @return  返回拼接后的字符串对象 
     */  
    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);  
    }
    
    /** 
     * 替换当前字符串对象中所有出现的oldChar字符为newChar字符,最终 
     * 返回一个新的字符串对象 
     * 若当前字符串对象中不包含oldChar字符,那么直接return this,否则, 
     * 会生成一个新的字符串对象并返回 
     * 示例: 
     * "mesquite in your cellar".replace('e', 'o') 
     *         returns "mosquito in your collar" 
     * "the war of baronets".replace('r', 'y') 
     *         returns "the way of bayonets" 
     * "sparring with a purple porpoise".replace('p', 't') 
     *         returns "starring with a turtle tortoise" 
     * "JonL".replace('q', 'x') 
     *         returns "JonL" (无变化) 
     * @param   oldChar   被替换的旧字符 
     * @param   newChar   替换的新字符 
     * @return  返回替换指定字符后新的字符串对象 
     */  
    public String replace(char oldChar, char newChar) {  
        //若被替换字符与替换字符不相等才会执行下面的操作,否则直接return this.  
        if (oldChar != newChar) {  
            int len = value.length;  
            int i = -1;  
            char[] val = value; /* 避免直接操作value属性 */  
  
            //从索引位置零开始查找当前字符串中第一次出现字符oldChar的索引位置  
            while (++i < len) {  
                if (val[i] == oldChar) {  
                    break;  
                }  
            }  
            // 若查找到oldChar在当前字符串中第一次出现的索引位置 < length(),  
            // 否则就没有字符替换的意义了  
            if (i < len) {  
                //将[0,i)直接的字符缓存到char[]字符数组中  
                char buf[] = new char[len];  
                for (int j = 0; j < i; j++) {  
                    buf[j] = val[j];  
                }  
                //遍历[i,length() - 1]之间的字符  
                while (i < len) {  
                    //获取遍历到的每个字符  
                    char c = val[i];  
                    // 若当前遍历到的字符与oldChar相等,则把当前遍历到的字符替换为newChar,  
                    // 否则当前遍历到的字符保持不变,然后将其存入char[]字符数组中  
                    buf[i] = (c == oldChar) ? newChar : c;  
                    i++;  
                }  
                // 最终根据临时变量char[]字符数组构造一个新的字符串对象并返回  
                return new String(buf, true);  
            }  
        }  
        return this;  
    }  
    
    /** 
     * 当且仅当当前字符串包含指定的字符序列才会返回true 
     * Returns true if and only if this string contains the specified 
     * sequence of char values. 
     * 
     * @param s    给定的字符序列对象 
     * @return true  若当前字符串包含给定的参数s表示的字符序列时返回true 
     * @throws NullPointerException 若参数s表示的字符串对象为null,则会抛出NullPointerException异常 
     * @since 1.5 
     */  
    public boolean contains(CharSequence s) {  
        return indexOf(s.toString()) > -1;  
    } 
    
        /** 
     * 根据给定的正则表达式来分割当前字符串并返回一个字符串数组 
     * 如果给定的正则表达式不匹配当前字符串,那么返回的字符串数组将只包含一个元素即 
     * 当前字符串对象.返回的字符串数组中的每个子字符串与它们在当前字符串中出现的顺序 
     * 保持一致. 
     * limit参数用于控制给定的正则表达式应用的次数,从而影响最终返回的字符串数组的长度. 
     * 若limit参数大于零,那么给定的正则表达式则会至多应用limit - 1次,同时最终返回的 
     * 字符串数组的长度将不大于limit,并且该数组的最后一个元素为前一个子字符串匹配后剩余 
     * 的部分。比如: 
     * "Hello world, Java"  -->  split(" ",2); 
     *         return: "Hello"  "world, Java" 
     * 若limit参数是一个负数,那么给定的正则表达式将会尽可能多的应用多次并且返回的字符串 
     * 数组的长度将会没有限制. 
     * 若limit参数为零,那么给定的正则表达式将会尽可能多的应用多次并且返回的字符串 
     * 数组的长度将会没有限制,尾部的空字符串将会被丢弃. 
     * 
     * <p> The string <tt>"boo:and:foo"</tt>, for example, yields the 
     * following results with these parameters: 
     * 
     * <blockquote><table cellpadding=1 cellspacing=0 summary="Split example showing regex, limit, and result"> 
     * <tr> 
     *     <th>Regex</th> 
     *     <th>Limit</th> 
     *     <th>Result</th> 
     * </tr> 
     * <tr><td align=center>:</td> 
     *     <td align=center>2</td> 
     *     <td><tt>{ "boo", "and:foo" }</tt></td></tr> 
     * <tr><td align=center>:</td> 
     *     <td align=center>5</td> 
     *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 
     * <tr><td align=center>:</td> 
     *     <td align=center>-2</td> 
     *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 
     * <tr><td align=center>o</td> 
     *     <td align=center>5</td> 
     *     <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr> 
     * <tr><td align=center>o</td> 
     *     <td align=center>-2</td> 
     *     <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr> 
     * <tr><td align=center>o</td> 
     *     <td align=center>0</td> 
     *     <td><tt>{ "b", "", ":and:f" }</tt></td></tr> 
     * </table></blockquote> 
     * 
     * <blockquote> 
     * {@link java.util.regex.Pattern}.{@link java.util.regex.Pattern#compile 
     * compile}<tt>(</tt><i>regex</i><tt>)</tt>.{@link 
     * java.util.regex.Pattern#split(java.lang.CharSequence,int) 
     * split}<tt>(</tt><i>str</i><tt>,</tt>&nbsp;<i>n</i><tt>)</tt> 
     * </blockquote> 
     * 
     * @param  regex  表示分隔符的正则表达式 
     * @param  limit  限制返回的字符串数组长度的阈值 
     * @return  返回当前字符串被分割后生成的字符串数组 
     * @throws  PatternSyntaxException 
     *          若给定的正则表达式语法错误会抛出此异常 
     * 
     * @see java.util.regex.Pattern 
     * 
     * @since 1.4 
     * @spec JSR-51 
     */  
    public String[] split(String regex, int limit) {  
        /** 
         * 分3种情况处理: 
         * 1. regex参数只有一个字符且该字符串不是正则表达式中的特殊字符 .$|()[{^?*+\ 或者 
         * 2. regex参数为2个字符,第一个字符是一个反斜杠\,第二个字符为非ASCII码字母或非ASCII码数字 
         * 3. regex参数超过2个字符并且为合法的正则表达式 
         * 前2种情况采用fastpath方式进行处理,后面第3种情况采用Pattern.split(this, limit)方法实现 
         */  
        char ch = 0;  
        //判定regex参数是否为上面所描述的前两种情况  
        if (((regex.value.length == 1 &&  
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||  
             (regex.length() == 2 &&  
              regex.charAt(0) == '\\' &&  
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&  
              ((ch-'a')|('z'-ch)) < 0 &&  
              ((ch-'A')|('Z'-ch)) < 0)) &&  
            (ch < Character.MIN_HIGH_SURROGATE ||  
             ch > Character.MAX_LOW_SURROGATE))  
        {  
            int off = 0;  
            int next = 0;  
            boolean limited = limit > 0;  
            ArrayList<String> list = new ArrayList<>();  
            while ((next = indexOf(ch, off)) != -1) {  
                if (!limited || list.size() < limit - 1) {  
                    //截取每一段子字符串存入list中  
                    list.add(substring(off, next));  
                    //修改截取的起始索引位置  
                    off = next + 1;  
                } else {  
                    //最后一个,只有设置的limit参数大于零,才会进入这个分支  
                    list.add(substring(off, value.length));  
                    off = value.length;  
                    break;  
                }  
            }  
            // off为零,当前字符串与给定的regex参数不匹配,那么直接返回一个字符串数组  
            // 并且该字符串数组只包含一个元素且该元素为当前字符串对象  
            if (off == 0)  
                return new String[]{this};  
  
            // 添加剩余的部分  
            if (!limited || list.size() < limit) {  
                list.add(substring(off, value.length));  
            }  
  
            // 构造最终的返回结果  
            int resultSize = list.size();  
            //若limit参数设置为零  
            if (limit == 0) {  
                //从尾部开始遍历,排除尾部连续的空字符串  
                while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {  
                    resultSize--;  
                }  
            }  
            String[] result = new String[resultSize];  
            //这里使用了list.subList方法,而该方法返回的SubList对象会在内部维护当前List对象,  
            //对SubList对象中的元素的操作会直接反映到其内部维护的List对象中,若SubList对象没有  
            //被回收,那么内部维护的List对象也不会被回收,内部维护的List中保存的对象也不会被回收,  
            //若内部维护的List是一个超大的集合,那么就会很容易发生OOM异常  
            //应该通过new ArrayList(list.subList(0, resultSize).toArray(result));  
            //这种方式来解决这种隐患  
            return list.subList(0, resultSize).toArray(result);  
        }  
        // 若regex参数超过2个字符并且是合法的正则表达式,那么直接调用Pattern类的.split(this, limit)  
        // 来完成实现  
        return Pattern.compile(regex).split(this, limit);  
    }  
  
    /** 
     * split(String regex, int limit)方法的重载,limit参数默认值为零 
     * 
     * @since 1.4 
     * @spec JSR-51 
     */  
    public String[] split(String regex) {  
        return split(regex, 0);  
    }  

4.2 比较方法

    /** 
     * 比较两个对象是否相等,当且仅当anObject参数不为null且 
     * 它与当前字符串对象表示的是同一个字符序列,才会返回true 
     * 
     * @param  anObject  与当前字符串对象进行比较的对象 
     * @return  返回指定对象是否与当前字符串对象相等 
     * 
     * @see  #compareTo(String) 
     * @see  #equalsIgnoreCase(String) 
     */  
    public boolean equals(Object anObject) {  
        if (this == anObject) {  
            return true;  
        }  
        //若anObject对象是String类型  
        if (anObject instanceof String) {  
            //强制转换成String对象  
            String anotherString = (String) anObject;  
            //获取当前字符串对象的字符序列并缓存到变量n上  
            int n = value.length;  
            //若两者的字符序列长度一致,则接下来要比较字符序列的每个字符是否相等  
            if (n == anotherString.value.length) {  
                char v1[] = value;  
                char v2[] = anotherString.value;  
                int i = 0;  
                while (n-- != 0) {  
                    //只要字符序列中存在一个字符不相等,则返回false即两者不相等  
                    if (v1[i] != v2[i]) {  
                        return false;  
                    }  
                    i++;  
                }  
                return true;  
            }  
        }  
        return false;  
    }  
  
    /** 
     * 比较当前字符串的字符序列是否与指定的StringBuffer对象包含的字符序列相等 
     * 
     * @param  sb   与当前字符串对象进行比较的StringBuffer对象 
     * @return  返回当前字符串对象是否与指定的StringBuffer对象相等 
     * 
     * @since  1.4 
     */  
    public boolean contentEquals(StringBuffer sb) {  
        synchronized (sb) {  
            return contentEquals((CharSequence) sb);  
        }  
    }  
  
    /** 
     * 比较当前字符串的字符序列是否与指定的CharSequence对象包含的字符序列相等 
     * @since  1.5 
     */  
    public boolean contentEquals(CharSequence cs) {  
        if (value.length != cs.length())  
            return false;  
        // 若传入的参数是StringBuffer或StringBuilder类型  
        if (cs instanceof AbstractStringBuilder) {  
            char v1[] = value;  
            char v2[] = ((AbstractStringBuilder) cs).getValue();  
            int i = 0;  
            int n = value.length;  
            while (n-- != 0) {  
                if (v1[i] != v2[i])  
                    return false;  
                i++;  
            }  
            return true;  
        }  
        // 若传入的参数是String类型  
        if (cs.equals(this))  
            return true;  
        // 若传入的参数是通用的CharSequence类型  
        char v1[] = value;  
        int i = 0;  
        int n = value.length;  
        while (n-- != 0) {  
            if (v1[i] != cs.charAt(i))  
                return false;  
            i++;  
        }  
        return true;  
    }  
  
    /** 
     * 比较当前字符串的字符序列是否与指定的String对象包含的字符序列相等, 
     * 此方法在比较过程中会忽略每个字符的大小写 
     * 
     * 当两个字符串c1和c2满足如下其中任意一个条件,则认为两者在忽略大小写的情况下相等: 
     *     1. c1 == c2 返回true 
     *     2. 对于两个字符串包含的字符序列中的每个字符调用java.lang.Character#toUpperCase(char)方法, 
     *        若最终生成的结果相同,则认为两者相等 
     *     3. 对于两个字符串包含的字符序列中的每个字符调用java.lang.Character#toLowerCase(char)方法, 
     *        若最终生成的结果相同,则认为两者相等 
     * @param  anotherString     与当前字符串对象进行比较的字符串对象 
     * @return  返回两个字符串对象是否相等 
     * 
     * @see  #equals(Object) 
     */  
    public boolean equalsIgnoreCase(String anotherString) {  
        return (this == anotherString) ? true  
                : (anotherString != null)  
                && (anotherString.value.length == value.length)  
                && regionMatches(true, 0, anotherString, 0, value.length);  
    }  
  
    /** 
     * 比较两个字符串的字母顺序 
     * 这个比较操作是基于字符串对象的字符序列中包含的每个字符的Unicode值进行比较。 
     * 如果当前字符串对象的字母顺序在字符串参数之前,那么返回结果为负数, 
     * 如果当前字符串对象的字母顺序在字符串参数之后,那么返回结果为正数, 
     * 如果当前字符串对象与字符串参数相等(即equals()返回true),那么返回结果为零 
     * 
     * @param   anotherString   the <code>String</code> to be compared. 
     * @return  返回两个字符串的比较结果, 
     *          正数表示当前字符串较大,负数表示当前字符串较小,否则表达两者相等 
     */  
    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++;  
        }  
        // 如果前lim个字符全部相等,那么此时就比较谁的字符串长度大,  
        // 判定长度大的字符串比长度小的字符串大  
        return len1 - len2;  
    }  
  
    /** 
     * 字符串忽略大小写比较器,此比较器实现了Serializable接口 
     * 注意:此接口并没有考虑语言环境,对于部分地区可能会导致不满意的顺序, 
     * java.text包下的Collators类提供了语言环境敏感的字符串顺序。 
     * 
     * @see     java.text.Collator#compare(String, String) 
     * @since   1.2 
     */  
    public static final Comparator<String> CASE_INSENSITIVE_ORDER  
                                         = new CaseInsensitiveComparator();  
    private static class CaseInsensitiveComparator  
            implements Comparator<String>, java.io.Serializable {  
        private static final long serialVersionUID = 8575799808933029326L;  
  
        public int compare(String s1, String s2) {  
            int n1 = s1.length();  
            int n2 = s2.length();  
            int min = Math.min(n1, n2);  
            for (int i = 0; i < min; i++) {  
                char c1 = s1.charAt(i);  
                char c2 = s2.charAt(i);  
                if (c1 != c2) {  
                    c1 = Character.toUpperCase(c1);  
                    c2 = Character.toUpperCase(c2);  
                    if (c1 != c2) {  
                        c1 = Character.toLowerCase(c1);  
                        c2 = Character.toLowerCase(c2);  
                        if (c1 != c2) {  
                            // 因为存在数字晋升机制,因此此处不会存在数据类型溢出问题  
                            return c1 - c2;  
                        }  
                    }  
                }  
            }  
            return n1 - n2;  
        }  
    }  
  
    /** 
     * 忽略大小写前提下比较两个字符串的字母顺序 
     * 注意:此接口并没有考虑语言环境,对于部分地区可能会导致不满意的顺序, 
     * java.text包下的Collators类提供了语言环境敏感的字符串顺序。 
     * 
     * @param   str   与当前字符串对象进行边角的字符串参数 
     * @return 
     *          返回负数表示指定的字符串对象大于当前字符串对象 
     *          返回正数表示指定的字符串对象小于当前字符串对象 
     *          返回零表示指定的字符串对象等于当前字符串对象 
     * @see     java.text.Collator#compare(String, String) 
     * @since   1.2 
     */  
    public int compareToIgnoreCase(String str) {  
        return CASE_INSENSITIVE_ORDER.compare(this, str);  
    }  
  
    /** 
     * 比较两个字符串的指定区域内的字符序列是否相等 
     * 注意:此方法并没有忽略字符大小写 
     * @param   toffset      当前字符串截取的起始索引位置 
     * @param   other        传入的另一个字符串对象 
     * @param   ooffset      other参数表示的字符串对象的起始索引位置 
     * @param   len          other参数表示的字符串对象的截取长度 
     * @return  返回两个字符串的指定区域内的字符序列是否相等 
     */  
    public boolean regionMatches(int toffset, String other, int ooffset,  
            int len) {  
        char ta[] = value;  
        int to = toffset;  
        char pa[] = other.value;  
        int po = ooffset;  
        // Note: toffset, ooffset, or len might be near -1>>>1.  
        if ((ooffset < 0) || (toffset < 0)  
                || (toffset > (long)value.length - len)  
                || (ooffset > (long)other.value.length - len)) {  
            return false;  
        }  
        while (len-- > 0) {  
            if (ta[to++] != pa[po++]) {  
                return false;  
            }  
        }  
        return true;  
    }  
  
    /** 
     * 比较两个字符串的指定区域内的字符序列是否相等 
     * 当且仅当满足以下其中任意一个提交, 
     * 则判定两个字符串的指定区域内的字符序列不相等: 
     *     1. toffset参数为负数 
     *     2. ooffset参数为负数 
     *     3. toffset + len大于当前字符串的长度 
     *     4. ooffset + len大于other参数表示的字符串的长度 
     *     5. ignoreCase=false且存在某些非负数K小于参数len, 
     *        比如:this.charAt(toffset + k) != other.charAt(ooffset + k) 
     *     6. ignoreCase=true且存在某些非负数K小于参数len, 
     *        比如: Character.toLowerCase(this.charAt(toffset + k)) != 
     *              Character.toLowerCase(other.charAt(ooffset + k)) 
     * 
     *              Character.toLowerCase(this.charAt(toffset+k)) != 
     *              Character.toLowerCase(other.charAt(ooffset+k)) 
     * 
     *              Character.toUpperCase(this.charAt(toffset + k)) != 
     *              Character.toUpperCase(other.charAt(ooffset + k)) 
     * 
     * @param   ignoreCase   比较过程中是否忽略大小写 
     * @param   toffset      当前字符串截取的起始索引位置 
     * @param   other        传入的另一个字符串对象 
     * @param   ooffset      other参数表示的字符串对象的起始索引位置 
     * @param   len          other参数表示的字符串对象的截取长度 
     * @return  返回两个字符串的指定区域内的字符序列是否相等 
     */  
    public boolean regionMatches(boolean ignoreCase, int toffset,  
            String other, int ooffset, int len) {  
        char ta[] = value;  
        int to = toffset;  
        char pa[] = other.value;  
        int po = ooffset;  
        // Note: toffset, ooffset, or len might be near -1>>>1.  
        if ((ooffset < 0) || (toffset < 0)  
                || (toffset > (long)value.length - len)  
                || (ooffset > (long)other.value.length - len)) {  
            return false;  
        }  
        while (len-- > 0) {  
            char c1 = ta[to++];  
            char c2 = pa[po++];  
            //如果两个字符相等,则跳过  
            if (c1 == c2) {  
                continue;  
            }  
            //如果两个字符不相等,则要判断是否设置了忽略大小写  
            if (ignoreCase) {  
                // 如果设置了忽略大小写,那么首先将两者统一转成大写再比较  
                char u1 = Character.toUpperCase(c1);  
                char u2 = Character.toUpperCase(c2);  
                //若转成大写后两者相等,那么跳过,继续比较下一个字符  
                if (u1 == u2) {  
                    continue;  
                }  
                //非常不幸的是,Character.toUpperCase()在转换成大写时对于格鲁吉亚字母不能正常工作,  
                //因为格鲁吉亚字母拥有怪异的大小写转换规则,因此我们需要在当前循环退出之前再进行一次字符比较检查  
                if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {  
                    continue;  
                }  
            }  
            // 若两个字符本身不相等且ignoreCase=false或  
            // 两个字符本身不相等且ignoreCase=true,但是统一转换成大写形式后仍然不相等,  
            // 此时应该返回false,即判定两个字符不相等  
            return false;  
        }  
        //否则判定两个字符串的指定区域内的字符序列相等  
        return true;  
    }  

4.3 hashCode方法

    /** 
     * 计算当前字符串的hashcode值,计算公式为: 
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 
     * 上面的n表示字符串的长度,^表示求幂运算 
     * 空字符串""的hashcode值为零 
     * 
     * @return  返回当前字符串对象的hashcode值 
     */  
    public int hashCode() {  
        int h = hash;  
        if (h == 0 && value.length > 0) {  
            char val[] = value;  
  
            for (int i = 0; i < value.length; i++) {  
                h = 31 * h + val[i];  
            }  
            hash = h;  
        }  
        return h;  
    } 

我们可以由上面的 for 循环推导出一个计算公式,hashCode 方法注释中已经给出:

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

hashCode方法为什么选择数字31作为乘子呢?

  • 31是一个素数,质数又称素数。指在一个大于1的自然数中,除了1和此整数自身外,没法被其他自然数整除的数。
  • 如果用过小的质数作为乘子算出的哈希值,数值不会很大。哈希值会分布在一个较小的数值区间内,分布性不佳,最终可能会导致冲突率上升。
  • 如果用过大的质数作为乘子算出的哈希值,数值会过大。如果用 int 类型表示哈希值,结果会溢出,最终导致数值信息丢失。
  • 31可以被 JVM 优化,31 * i = (i << 5) - i,并且31只占用5bits。
  • 31作为乘子计算出来的数值不大不小,在Java中,整型数是32位的,也就是说最多有2^32= 4294967296个整数,将任意一个字符串,经过hashCode计算之后,得到的整数应该在这4294967296数之中。那么,最多有 4294967297个不同的字符串作hashCode之后,肯定有两个结果是一样的, hashCode可以保证相同的字符串的hash值肯定相同,但是,hash值相同并不一定是value值就相同

在存储数据计算hash地址的时候,我们希望尽量减少有同样的hash地址,所谓“冲突”。如果使用相同hash地址的数据过多,那么这些数据所组成的hash链就更长,从而降低了查询效率!所以在选择系数的时候要选择尽量长的系数并且让乘法尽量不要溢出的系数,因为如果计算出来的hash地址越大,所谓的“冲突”就越少,查找起来效率也会提高。

4.4 substring方法

    /** 
     * 从beginIndex参数表示的索引位置开始截取当前字符串对象 
     * 若beginIndex=0,则直接返回当前字符串对象,否则截取生成一个子字符串对象 
     */  
    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);  
    }  
    
    /** 
     * 截取[beginIndex,endIndex)之间的字符串 
     * 最终生成的子字符串的长度为endIndex - beginIndex 
     * 
     * @param      beginIndex   截取的起始索引位置,包含在内 
     * @param      endIndex     截取的结束索引位置,不包含在内 
     * @return     返回截取的子字符串对象 
     * @exception  IndexOutOfBoundsException  如果 
     *             beginIndex为负数或 
     *             endIndex > length()或 
     *             beginIndex > endIndex,则会抛出索引越界异常 
     */  
    public String substring(int beginIndex, int endIndex) {  
        if (beginIndex < 0) {  
            throw new StringIndexOutOfBoundsException(beginIndex);  
        }  
        if (endIndex > value.length) {  
            throw new StringIndexOutOfBoundsException(endIndex);  
        }  
        int subLen = endIndex - beginIndex;  
        if (subLen < 0) {  
            throw new StringIndexOutOfBoundsException(subLen);  
        }  
        return ((beginIndex == 0) && (endIndex == value.length)) ? this  
                : new String(value, beginIndex, subLen);  
    }  

4.5 replaceFirst、replaceAll、replace方法

    /** 
     * 根据给定的正则表达式替换当前字符串匹配到的第一个子字符串为参数replacement表示的字符串 
     * 注意:replacement参数中若包含反斜杠\和美元符号$可能会与直接将其当做纯文本替换字符串 
     * 返回的结果不一致.如果你需要抑制特殊字符的潜在含义,那么你可能会需要使用 
     * java.util.regex.Matcher类的quoteReplacement方法来实现当前方法的功能。默认情况下, 
     * 当前方法是采用java.util.regex.Matcher类的replaceFirst方法实现的. 
     * 
     * @param   regex         给定的正则表达式 
     * @param   replacement   替换字符串 
     * @return  String 
     * @throws  PatternSyntaxException 
     *          若给定的正则表达式语法错误会抛出此异常 
     * 
     * @see java.util.regex.Pattern 
     * 
     * @since 1.4 
     * @spec JSR-51 
     */  
    public String replaceFirst(String regex, String replacement) {  
        return Pattern.compile(regex).matcher(this).replaceFirst(replacement);  
    }  
  
    /** 
     * 根据给定的正则表达式替换当前字符串匹配到的所有子字符串为参数replacement表示的字符串 
     * 注意:replacement参数中若包含反斜杠\和美元符号$可能会与直接将其当做纯文本替换字符串 
     * 返回的结果不一致.如果你需要抑制特殊字符的潜在含义,那么你可能会需要使用 
     * java.util.regex.Matcher类的quoteReplacement方法来实现当前方法的功能。默认情况下, 
     * 当前方法是采用java.util.regex.Matcher类的replaceAll方法实现的. 
     * 
     * @param   regex         给定的正则表达式 
     * @param   replacement   替换字符串 
     * @return  String 
     * @throws  PatternSyntaxException 
     *          若给定的正则表达式语法错误会抛出此异常 
     * 
     * @see java.util.regex.Pattern 
     * 
     * @since 1.4 
     * @spec JSR-51 
     */  
    public String replaceAll(String regex, String replacement) {  
        return Pattern.compile(regex).matcher(this).replaceAll(replacement);  
    }  

    /** 
     * 将当前字符串中第一次出现的target字符序列替换为replacement字符序列 
     * 注意:若target或replacement参数中包含了正则表达式中的特殊字符,会一律 
     * 按照纯文本字符去理解,不会被理解为它在正则表达式中表达的含义, 
     * 即当前方法不支持正则表达式. 
     * @param  target         被替换的字符序列 
     * @param  replacement    替换的字符序列 
     * @return  The resulting string 
     * @throws NullPointerException if <code>target</code> or 
     *         <code>replacement</code> is <code>null</code>. 
     * @since 1.5 
     */  
    public String replace(CharSequence target, CharSequence replacement) {  
        return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(  
                this).replaceAll(Matcher.quoteReplacement(replacement.toString()));  
    } 

4.6 copyValueOf、 valueOf方法

    /** 
     * 将指定的字符数组的[offset,offset + count]区间内的字符序列转换成一个字符串对象 
     * 同valueOf(char data[], int offset, int count)方法实现细节一模一样,仅仅只是方法名称不同 
     */  
    public static String copyValueOf(char data[], int offset, int count) {  
        return new String(data, offset, count);  
    }  
  
    /** 
     * 将指定的字符数组转换成一个字符串对象 
     * 同valueOf(char data[])方法的具体实现细节一模一样,仅仅只是方法名称不同 
     * 
     * @param   data   字符数组对象 
     * @return  返回一个新的字符串对象 
     */  
    public static String copyValueOf(char data[]) {  
        return new String(data);  
    }  
    
    /** 
     * 将指定的obj对象转换成String对象 
     * 
     * @param   obj   任意的Object对象 
     * @return  若obj == null,则会返回字符串"null",否则返回obj.toString() 
     * @see     java.lang.Object#toString() 
     */  
    public static String valueOf(Object obj) {  
        return (obj == null) ? "null" : obj.toString();  
    }  
  
    /** 
     * 将指定的字符数组对象转换成String对象 
     * 
     * @param   data   字符数组对象 
     * @return  返回一个新的字符串对象 
     */  
    public static String valueOf(char data[]) {  
        return new String(data);  
    }  
  
    /** 
     * 将指定的字符数组的[offset,offset + count]区间内的字符序列转换成一个字符串对象 
     * 
     * @param   data     字符数组 
     * @param   offset   截取的起始偏移量 
     * @param   count    截取的字符个数 
     * @return  返回一个包含给定字符数组指定区间内的字符序列的字符串对象 
     * @exception IndexOutOfBoundsException 
     *            若offset为负数或 
     *            count为负数或 
     *            offset + count > data.length(), 
     *            则会抛出IndexOutOfBoundsException数组索引位置越界异常 
     */  
    public static String valueOf(char data[], int offset, int count) {  
        return new String(data, offset, count);  
    }  
    
    /** 
     * 将boolean变量值转换成一个字符串对象 
     * 
     * @param   b   一个boolean值 
     * @return  true   --> "true" 
     *          false  --> "false" 
     */  
    public static String valueOf(boolean b) {  
        return b ? "true" : "false";  
    }  
  
    /** 
     * 将单个字符转换成一个字符串对象 
     * 
     * @param   c   一个字符 
     * @return  返回单个字符转换后得到的一个长度为1的字符串对象 
     */  
    public static String valueOf(char c) {  
        char data[] = {c};  
        return new String(data, true);  
    }  
  
    /** 
     * 将int数字转换成一个字符串对象 
     * 内部实际是调用Integer.toString()方法实现的 
     * 
     * @param   i   int数字 
     * @return  返回一个int类型的数字的字符串表示形式 
     * @see     java.lang.Integer#toString(int, int) 
     */  
    public static String valueOf(int i) {  
        return Integer.toString(i);  
    }  
  
    /** 
     * 将long数字转换成一个字符串对象 
     * 内部实际是调用Long.toString()方法实现的 
     * 
     * @param   l   long类型的数字 
     * @return  返回一个long类型的数字的字符串表示形式 
     * @see     java.lang.Long#toString(long) 
     */  
    public static String valueOf(long l) {  
        return Long.toString(l);  
    }  
    
    /** 
     * 将float数字转换成一个字符串对象 
     * 内部实际是调用Float.toString()方法实现的 
     * 
     * @param   f   float类型的数字 
     * @return  返回一个float类型的数字的字符串表示形式 
     * @see     java.lang.Float#toString(float) 
     */  
    public static String valueOf(float f) {  
        return Float.toString(f);  
    }  
  
    /** 
     * 将double数字转换成一个字符串对象 
     * 内部实际是调用Double.toString()方法实现的 
     * 
     * @param   d   double类型的数字 
     * @return  返回一个double类型的数字的字符串表示形式 
     * @see     java.lang.Double#toString(double) 
     */  
    public static String valueOf(double d) {  
        return Double.toString(d);  
    }  

4.7 intern方法

    /** 
     * 返回字符串对象的规范化表示形式 
     * 字符串常量池初始化默认是空的,它由String类私下管理维护 
     * 当intern方法被执行,如果字符串常量池已经包含了与当前字符串对象equals的字符串, 
     * 那么直接从字符串常量池里返回该字符串对象即可,否则会将该字符串对象添加到常量池中 
     * 并返回. 
     * 因此对于任意的两个字符串s和t, 
     * 当且仅当s.equals(t) == true, 
     * s.intern() == t.intern()才会返回true. 
     * 所有的字符串字面量以及基于String的常量表达式都会被字符串常量池集中管理和返回. 
     * 
     * @return  从字符串常量池返回一个字符串,保持返回的字符串是在常量池中并且是唯一的 
     */  
    public native String intern();  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值