/*
* Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
import java.io.ObjectStreamField;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Formatter;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* The <code>String</code> class represents character strings. All
* string literals in Java programs, such as <code>"abc"</code>, are
* implemented as instances of this class.
* <p>
* Strings are constant; their values cannot be changed after they
* are created. String buffers support mutable strings.
* Because String objects are immutable they can be shared. For example:
* <p><blockquote><pre>
* String str = "abc";
* </pre></blockquote><p>
* is equivalent to:
* <p><blockquote><pre>
* char data[] = {'a', 'b', 'c'};
* String str = new String(data);
* </pre></blockquote><p>
* Here are some more examples of how strings can be used:
* <p><blockquote><pre>
* System.out.println("abc");
* String cde = "cde";
* System.out.println("abc" + cde);
* String c = "abc".substring(2,3);
* String d = cde.substring(1, 2);
* </pre></blockquote>
* <p>
* The class <code>String</code> includes methods for examining
* individual characters of the sequence, for comparing strings, for
* searching strings, for extracting substrings, and for creating a
* copy of a string with all characters translated to uppercase or to
* lowercase. Case mapping is based on the Unicode Standard version
* specified by the {@link java.lang.Character Character} class.
* <p>
* The Java language provides special support for the string
* concatenation operator ( + ), and for conversion of
* other objects to strings. String concatenation is implemented
* through the <code>StringBuilder</code>(or <code>StringBuffer</code>)
* class and its <code>append</code> method.
* String conversions are implemented through the method
* <code>toString</code>, defined by <code>Object</code> and
* inherited by all classes in Java. For additional information on
* string concatenation and conversion, see Gosling, Joy, and Steele,
* <i>The Java Language Specification</i>.
*
* <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
* or method in this class will cause a {@link NullPointerException} to be
* thrown.
*
* <p>A <code>String</code> represents a string in the UTF-16 format
* in which <em>supplementary characters</em> are represented by <em>surrogate
* pairs</em> (see the section <a href="Character.html#unicode">Unicode
* Character Representations</a> in the <code>Character</code> class for
* more information).
* Index values refer to <code>char</code> code units, so a supplementary
* character uses two positions in a <code>String</code>.
* <p>The <code>String</code> class provides methods for dealing with
* Unicode code points (i.e., characters), in addition to those for
* dealing with Unicode code units (i.e., <code>char</code> values).
*
* @author Lee Boynton
* @author Arthur van Hoff
* @author Martin Buchholz
* @author Ulf Zibis
* @see java.lang.Object#toString()
* @see java.lang.StringBuffer
* @see java.lang.StringBuilder
* @see java.nio.charset.Charset
* @since JDK1.0
*/
/**
* String类表示一类字符串,Java程序中的字符串都是当前类的实例,并且都是不可变的常量,
* 它们一旦被创建就不能再被修改。字符串缓冲区支持可变的字符串,
*
*/
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** 用于存储当前字符串包含的字符 */
private final char value[];
/** 用于缓存当前字符串的HashCod值,默认值为零**/
private int hash;
private static final long serialVersionUID = -6849794470754667710L;
/**
* 这个类主要用来提取序列化过程中某个对象内的字段【成员属性】元数据信息,
* 包括字段的类型、类型代码、签名等
*/
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
/**
* 初始化创建一个新的String对象用来表示一个空字符串。
* 注意:完全没必要使用此构造器来创建一个String对象,因为String自身已经被设计为不可变
*/
public String() {
this.value = new char[0];
}
/**
* 通过传入一个字符串参数来构建一个空的String对象,换句话说,新创建的字符串对象是
* 传入的字符串参数的一个副本。除非你确实需要显式的复制一个字符串对象,否则你完全
* 没必要使用此构造器来创建一个String对象,因为String自身已经被设计为不可变
* @param original 原始的字符串对象
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
/**
* 通过传入的一个字符数组来构建一个空的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) {
throw new StringIndexOutOfBoundsException(count);
}
// 如果offset + count > value.length,则会抛出字符串越界异常
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;
}
/**
* 通过传入一个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());
}
}
/**
* 根据传入的StringBuilder对象构造一个新的String对象,内部会将StringBuilder对象
* 内的字符数组都复制到当前对象的value属性上,注意此方法不是线程安全的。
* @since 1.5
*/
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
String(char[] value, boolean share) {
this.value = value;
}
/**
* 私有构造函数,建议使用String(char[],int,int)构造函数代替,
* 此构造函数已被标记为废弃
*/
@Deprecated
String(int offset, int count, char[] value) {
this(value, offset, count);
}
/**
* 返回一个字符串的长度,此长度必须等于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);
}
/**
* index索引位置的字符往后偏移codePointOffset个索引位置,返回偏移后的索引位置
*
* @param index 某个字符的索引位置,是个基准点
* @param codePointOffset Unicode代码点的偏移量
* @return index 返回index索引位置偏移codePointOffset个Unicode代码点后新的索引位置
* @exception IndexOutOfBoundsException
* 若index参数为负数或大于length()或positive是正数且
* index索引位置后面的子字符串包含的Unicode代码点个数
* 小于codePointOffset或codePointOffset是负数且index索引位置前面的
* 子字符串包含的Unicode代码点个数小于codePointOffset参数的绝对值,
* 则会抛出索引位置越界异常
* @since 1.5
*/
public int offsetByCodePoints(int index, int codePointOffset) {
if (index < 0 || index > value.length) {
throw new IndexOutOfBoundsException();
}
return Character.offsetByCodePointsImpl(value, 0, value.length,
index, codePointOffset);
}
/**
* 将当前字符串的字符复制到另一个目标字符数组中
* @param dst 目标字符数组
* @param dstBegin 复制的字符从目标字符数组的哪个索引位置开始放入
*/
void getChars(char dst[], int dstBegin) {
System.arraycopy(value, 0, dst, dstBegin, value.length);
}
/**
* 将当前字符串的[srcBegin,srcEnd)区间内的字符复制到dst[]目标字符数组内
* dstbegin + (srcEnd-srcBegin) - 1
*
* @param srcBegin 从原字符串的字符数组的哪个索引位置开始复制
* @param srcEnd 从原字符串的字符数组的哪个索引位置开始结束复制
* @param dst 字符复制到的哪个目标字符数组
* @param dstBegin 复制的字符从目标字符数组的哪个索引位置开始放入
* @exception IndexOutOfBoundsException
* 若srcBegin参数为负数或
* srcBegin参数大于srcEnd参数或
* srcEnd大于length()或
* dstBegin参数为负数或
* dstBegin + (srcEnd - srcBegin)表达式计算值大于dst.length(),
* 则会抛出索引位置越界异常
*/
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
/**
* 将字符串的包含字符复制到一个目标的字节数组中。每个字节接收对应字符的低8位,
* 而每个字符的高8位则不会被复制,也不会参与以任何方式的转换
*
* @deprecated 此方法不能准确的将字符转换成byte字节数组。
* 自JDK1.1版本起,推荐使用String的getBytes()方法来代替,
* getBytes()方法默认会使用系统平台的默认字符集编码
*
* @param srcBegin 从原字符串的字符数组的哪个索引位置开始复制
* @param srcEnd 从原字符串的字符数组的哪个索引位置开始结束复制
* @param dst 字符复制到的哪个目标字节数组
* @param dstBegin 复制的字符从目标字节数组的哪个索引位置开始放入
* @exception IndexOutOfBoundsException
* 若srcBegin参数为负数或
* srcBegin参数大于srcEnd参数或
* srcEnd大于length()或
* dstBegin参数为负数或
* dstBegin + (srcEnd - srcBegin)表达式计算值大于dst.length(),
* 则会抛出索引位置越界异常
*/
@Deprecated
public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
int j = dstBegin;
int n = srcEnd;
int i = srcBegin;
/** 避免直接操作类的属性,使用方法中的局部变量代替引用可以减少getfield操作的次数,提高性能*/
char[] val = value;
while (i < n) {
dst[j++] = (byte)val[i++];
}
}
/**
* 使用指定的字符集编码将String对象编码成一个字节数组
* 若charsetName参数未指定,那么内部会抛出NullPointerException异常
* 如果你想要更详细的控制字符编码的处理过程,那么你需要使用{@link java.nio.charset.CharsetEncoder}类
*
* @param charsetName
* {@linkplain java.nio.charset.Charset}类支持的字符集编码名称
* @return 返回String对象编码后的字节数组
* @throws UnsupportedEncodingException
* charset参数表示的不是合法的字符集编码名称,
* 那么会抛出UnsupportedEncodingException异常
*
* @since JDK1.1
*/
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException {
if (charsetName == null) throw new NullPointerException();
return StringCoding.encode(charsetName, value, 0, value.length);
}
/**
* 方法同上,只是字符集编码参数改成了通过Charset类来提供
*
* @since 1.6
*/
public byte[] getBytes(Charset charset) {
if (charset == null) throw new NullPointerException();
return StringCoding.encode(charset, value, 0, value.length);
}
/**
* 同getBytes(String charsetName)方法类似,只是这里是采用系统平台默认的字符集编码
* 对字符串进行编码
*
* @since JDK1.1
*/
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}
/**
* 比较两个对象是否相等,当且仅当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;
}
/**
* 检测当前字符串是否以指定的前缀字符串开头
* 此方法等价于this.substring(toffset).startsWith(prefix)
* @param prefix 指定的字符串前缀
* @param toffset 起始偏移量
* @return 若toffset参数为负数或者大于当前字符串的长度,则直接返回false
*/
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;
// 若toffset为负数或toffset + prefix.length() > this.length(),则直接return false
if ((toffset < 0) || (toffset > value.length - pc)) {
return false;
}
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
return true;
}
/**
* 检测当前字符串是否以指定的前缀字符串开头(重载)
* @param prefix 指定的字符串前缀
* @since 1. 0
*/
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
/**
* 检测当前字符串是否以指定的后缀字符串结尾
*
* @param suffix 后缀字符串
*/
public boolean endsWith(String suffix) {
return startsWith(suffix, value.length - suffix.value.length);
}
/**
* 计算当前字符串的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;
}
/**
* 返回指定字符在当前字符串对象中第一次出现的索引位置
*
* @param ch Unicode code point表示的一个字符,取值范围为[0, 0xFFFF)
* @return 若指定字符在当前字符串的字符序列中不存在,则返回-1
*/
public int indexOf(int ch) {
return indexOf(ch, 0);
}
/**
* 返回指定字符在当前字符串对象中第一次出现的索引位置
*
* @param ch Unicode code point表示的一个字符,取值范围为[0, 0xFFFF)
* @param fromIndex 在当前字符串中查找的起始索引位置
* @return 若指定字符在当前字符串的字符序列中不存在,则返回-1
*/
public int indexOf(int ch, int fromIndex) {
final int max = value.length;
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= max) {
return -1;
}
//若ch参数小于Unicode补充代码点的最小值0x010000
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// 查理大部分情况,比如ch参数是一个合法的BMP代码点,
// 或者ch是一个负数即非法的代码点
final char[] value = this.value;
for (int i = fromIndex; i < max; i++) {
if (value[i] == ch) {
return i;
}
}
return -1;
} else {
//字符属于Unicode补充代码点情况
return indexOfSupplementary(ch, fromIndex);
}
}
/**
* 返回指定的代码点表示的字符在当前字符串中第一次出现的索引位置
* @param fromIndex 从哪个索引位置开始查找
*/
private int indexOfSupplementary(int ch, int fromIndex) {
if (Character.isValidCodePoint(ch)) {
final char[] value = this.value;
/**
* 一个完整的Unicode字符叫代码点CodePoint,而一个Java char叫代码单元code unit。
* String对象以UTF-16保存Unicode字符,需要用2个字符表示一个超大字符集的汉字,
* 这这种表示方式称之为Surrogate,第一个字符叫 Surrogate High,第二个就是 Surrogate Low
*/
final char hi = Character.highSurrogate(ch);
final char lo = Character.lowSurrogate(ch);
final int max = value.length - 1;
for (int i = fromIndex; i < max; i++) {
if (value[i] == hi && value[i + 1] == lo) {
return i;
}
}
}
return -1;
}
/**
* 返回指定代码点表示的字符在当前字符串对象中最后一次出现的索引位置
*/
public int lastIndexOf(int ch) {
return lastIndexOf(ch, value.length - 1);
}
/**
* 返回指定的代码点表示的字符在当前字符串中最后一次出现的索引位置
* 与indexOf方法类似
*/
public int lastIndexOf(int ch, int fromIndex) {
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
final char[] value = this.value;
int i = Math.min(fromIndex, value.length - 1);
for (; i >= 0; i--) {
if (value[i] == ch) {
return i;
}
}
return -1;
} else {
return lastIndexOfSupplementary(ch, fromIndex);
}
}
/**
* 处理补充字符的最后一次出现索引位置计算
*/
private int lastIndexOfSupplementary(int ch, int fromIndex) {
if (Character.isValidCodePoint(ch)) {
final char[] value = this.value;
char hi = Character.highSurrogate(ch);
char lo = Character.lowSurrogate(ch);
int i = Math.min(fromIndex, value.length - 2);
for (; i >= 0; i--) {
if (value[i] == hi && value[i + 1] == lo) {
return i;
}
}
}
return -1;
}
/**
* indexOf方法的重载
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
/**
* 返回指定字符串在当前字符串对象中第一次出现的索引位置
*/
public int indexOf(String str, int fromIndex) {
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/**
* 返回指定目标字符数组的[targetOffset,targetOffset + targetCount]区间内字符序列
* 在源字符数组的[sourceOffset,sourceOffset + sourceCount]区间内字符序列中
* 第一次出现的索引位置
*
* @param source 待搜索的源字符数组
* @param sourceOffset 源字符数组搜索的起始偏移量
* @param sourceCount 源字符数组从起始偏移量位置开始往后搜索的字符个数
* @param target 待搜索的目标字符数组
* @param targetOffset 目标字符数组搜索的起始偏移量
* @param targetCount 目标字符数组从起始偏移量位置开始往后搜索的字符个数
* @param fromIndex 开始搜索的起始索引位置
*/
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount);
for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* 查找第一个字符 */
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j]
== target[k]; j++, k++);
if (j == end) {
/* 到底目标字符结尾 */
return i - sourceOffset;
}
}
}
return -1;
}
/**
* 返回指定字符串在当前字符串对象中的最后一次出现的索引位置
*/
public int lastIndexOf(String str) {
return lastIndexOf(str, value.length);
}
/**
* 返回指定字符串在当前字符串对象中的最后一次出现的索引位置,
* 在当前字符串上从指定的fromIndex索引位置开始搜索
*/
public int lastIndexOf(String str, int fromIndex) {
return lastIndexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/**
* 返回指定目标字符数组的[targetOffset,targetOffset + targetCount]区间内字符序列
* 在源字符数组的[sourceOffset,sourceOffset + sourceCount]区间内字符序列中
* 最后一次出现的索引位置
*
* @param source 待搜索的源字符数组
* @param sourceOffset 源字符数组搜索的起始偏移量
* @param sourceCount 源字符数组从起始偏移量位置开始往后搜索的字符个数
* @param target 待搜索的目标字符数组
* @param targetOffset 目标字符数组搜索的起始偏移量
* @param targetCount 目标字符数组从起始偏移量位置开始往后搜索的字符个数
* @param fromIndex 开始搜索的起始索引位置
*/
static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
int rightIndex = sourceCount - targetCount;
if (fromIndex < 0) {
return -1;
}
if (fromIndex > rightIndex) {
fromIndex = rightIndex;
}
/* 若targetCount==0,则直接返回fromIndex */
if (targetCount == 0) {
return fromIndex;
}
int strLastIndex = targetOffset + targetCount - 1;
char strLastChar = target[strLastIndex];
int min = sourceOffset + targetCount - 1;
int i = min + fromIndex;
startSearchForLastChar:
while (true) {
while (i >= min && source[i] != strLastChar) {
i--;
}
if (i < min) {
return -1;
}
int j = i - 1;
int start = j - (targetCount - 1);
int k = strLastIndex - 1;
while (j > start) {
if (source[j--] != target[k--]) {
i--;
continue startSearchForLastChar;
}
}
return start - sourceOffset + 1;
}
}
/**
* 从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);
}
/**
* 截取[beginIndex,endIndex)之间的字符串,最终生成CharSequence对象
* 与substring(int beginIndex, int endIndex)方法类似,区别就是返回值类型不同
*
* @param beginIndex 截取的起始索引位置,包含在内
* @param endIndex 截取的结束索引位置,不包含在内
* @return 返回截取的CharSequence对象
* @exception IndexOutOfBoundsException 如果
* beginIndex为负数或
* endIndex > length()或
* beginIndex > endIndex,则会抛出索引越界异常
*/
public CharSequence subSequence(int beginIndex, int endIndex) {
return this.substring(beginIndex, endIndex);
}
/**
* 将指定的字符串拼接到当前字符串的末尾
* 若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;
}
/**
* 检测当前字符串是否符合给定的正则表达式
*
* @param regex 当前字符串对象用来匹配的正则表达式
* @return 当且仅当当前字符串匹配给定的正则表达式才会返回true
* @throws PatternSyntaxException
* 若给定的正则表达式语法错误会抛出此异常
*
* @see java.util.regex.Pattern
*
* @since 1.4
* @spec JSR-51
*/
public boolean matches(String regex) {
return Pattern.matches(regex, 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;
}
/**
* 根据给定的正则表达式替换当前字符串匹配到的第一个子字符串为参数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()));
}
/**
* 根据给定的正则表达式来分割当前字符串并返回一个字符串数组
* 如果给定的正则表达式不匹配当前字符串,那么返回的字符串数组将只包含一个元素即
* 当前字符串对象.返回的字符串数组中的每个子字符串与它们在当前字符串中出现的顺序
* 保持一致.
* 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> <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);
}
/**
* 使用给定Local的大小写形式的转换规则将当前字符串对象的字符序列中包含的每个字符转换成小写形式
* 由于大小写映射并不总是以1:1的形式进行字符映射,因此可能会导致转换后的字符串长度与原字符串的长度
* 不一致.
*
* Examples of lowercase mappings are in the following table:
* <table border="1" summary="Lowercase mapping examples showing language code of locale, upper case, lower case, and description">
* <tr>
* <th>Language Code of Locale</th>
* <th>Upper Case</th>
* <th>Lower Case</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>tr (Turkish)</td>
* <td>\u0130</td>
* <td>\u0069</td>
* <td>capital letter I with dot above -> small letter i</td>
* </tr>
* <tr>
* <td>tr (Turkish)</td>
* <td>\u0049</td>
* <td>\u0131</td>
* <td>capital letter I -> small letter dotless i </td>
* </tr>
* <tr>
* <td>(all)</td>
* <td>French Fries</td>
* <td>french fries</td>
* <td>lowercased all chars in String</td>
* </tr>
* <tr>
* <td>(all)</td>
* <td><img src="doc-files/capiota.gif" alt="capiota"><img src="doc-files/capchi.gif" alt="capchi">
* <img src="doc-files/captheta.gif" alt="captheta"><img src="doc-files/capupsil.gif" alt="capupsil">
* <img src="doc-files/capsigma.gif" alt="capsigma"></td>
* <td><img src="doc-files/iota.gif" alt="iota"><img src="doc-files/chi.gif" alt="chi">
* <img src="doc-files/theta.gif" alt="theta"><img src="doc-files/upsilon.gif" alt="upsilon">
* <img src="doc-files/sigma1.gif" alt="sigma"></td>
* <td>lowercased all chars in String</td>
* </tr>
* </table>
*
* @param locale 语言环境对象,不同的语言环境下的大小写字符转换规则不同
* @return 返回当前字符串对象的小写形式
* @see java.lang.String#toLowerCase()
* @see java.lang.String#toUpperCase()
* @see java.lang.String#toUpperCase(Locale)
* @since 1.1
*/
public String toLowerCase(Locale locale) {
//若locale参数为null,则直接抛出NullPointerException异常
if (locale == null) {
throw new NullPointerException();
}
int firstUpper;
final int len = value.length;
//先扫描出前面本身已经是小写形式的字符
scan: {
for (firstUpper = 0 ; firstUpper < len; ) {
char c = value[firstUpper];
//若当前字符在High Surrogate的字符范围内
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
int supplChar = codePointAt(firstUpper);
if (supplChar != Character.toLowerCase(supplChar)) {
break scan;
}
//通过Character.charCount计算实际字符的个数
firstUpper += Character.charCount(supplChar);
} else {
if (c != Character.toLowerCase(c)) {
break scan;
}
firstUpper++;
}
}
return this;
}
char[] result = new char[len];
int resultOffset = 0;
/* 复制第一个小写字符 */
System.arraycopy(value, 0, result, 0, firstUpper);
String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] lowerCharArray;
int lowerChar;
int srcChar;
int srcCount;
//从firstUpper索引位置开始,后面的字符都是需要进行小写处理的
for (int i = firstUpper; i < len; i += srcCount) {
srcChar = (int)value[i];
//若当前字符是HIGH SURROGATE
if ((char)srcChar >= Character.MIN_HIGH_SURROGATE
&& (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
//获取实际的Unicode代码点
srcChar = codePointAt(i);
//计算实际字符长度
srcCount = Character.charCount(srcChar);
} else {
srcCount = 1;
}
//考虑特殊情况
if (localeDependent ||
srcChar == '\u03A3' || // 希腊大写字母σ
srcChar == '\u0130') { // 拉丁大写字母I
lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
} else {
//一般情况,直接Character.toLowerCase()方式转换成小写
lowerChar = Character.toLowerCase(srcChar);
}
//若转换后得到的是错误字符,或者是一个Unicode补充代码点
if ((lowerChar == Character.ERROR)
|| (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (lowerChar == Character.ERROR) {
lowerCharArray =
ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
} else if (srcCount == 2) {
resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
continue;
} else {
lowerCharArray = Character.toChars(lowerChar);
}
//得到最终小写字符数组的长度
int mapLen = lowerCharArray.length;
//如果大于原字符串长度
if (mapLen > srcCount) {
//小写字符数组扩容
char[] result2 = new char[result.length + mapLen - srcCount];
//result --> result2
System.arraycopy(result, 0, result2, 0, i + resultOffset);
result = result2;
}
for (int x = 0; x < mapLen; ++x) {
result[i + resultOffset + x] = lowerCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)lowerChar;
}
}
return new String(result, 0, len + resultOffset);
}
/**
* toLowerCase(Locale locale)方法的重载,
* Local默认值为Locale.getDefault(),默认的Local跟你
* 当前JVM实例运行的主机系统平台环境有关
* @see java.lang.String#toLowerCase(Locale)
*/
public String toLowerCase() {
return toLowerCase(Locale.getDefault());
}
/**
* 使用给定Local的大小写形式的转换规则将当前字符串对象的字符序列中包含的每个字符转换成大写形式
* 由于大小写映射并不总是以1:1的形式进行字符映射,因此可能会导致转换后的字符串长度与原字符串的长度
* 不一致.
* @param locale 语言环境对象,不同的语言环境下的大小写字符转换规则不同
* @return 返回当前字符串对象的大写形式
* @see java.lang.String#toUpperCase()
* @see java.lang.String#toLowerCase()
* @see java.lang.String#toLowerCase(Locale)
* @since 1.1
*/
public String toUpperCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstLower;
final int len = value.length;
scan: {
for (firstLower = 0 ; firstLower < len; ) {
int c = (int)value[firstLower];
int srcCount;
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
c = codePointAt(firstLower);
srcCount = Character.charCount(c);
} else {
srcCount = 1;
}
int upperCaseChar = Character.toUpperCaseEx(c);
if ((upperCaseChar == Character.ERROR)
|| (c != upperCaseChar)) {
break scan;
}
firstLower += srcCount;
}
return this;
}
char[] result = new char[len]; /* may grow */
int resultOffset = 0; /* result may grow, so i+resultOffset
* is the write location in result */
/* Just copy the first few upperCase characters. */
System.arraycopy(value, 0, result, 0, firstLower);
String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] upperCharArray;
int upperChar;
int srcChar;
int srcCount;
for (int i = firstLower; i < len; i += srcCount) {
srcChar = (int)value[i];
if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
(char)srcChar <= Character.MAX_HIGH_SURROGATE) {
srcChar = codePointAt(i);
srcCount = Character.charCount(srcChar);
} else {
srcCount = 1;
}
if (localeDependent) {
upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
} else {
upperChar = Character.toUpperCaseEx(srcChar);
}
if ((upperChar == Character.ERROR)
|| (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (upperChar == Character.ERROR) {
if (localeDependent) {
upperCharArray =
ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
} else {
upperCharArray = Character.toUpperCaseCharArray(srcChar);
}
} else if (srcCount == 2) {
resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;
continue;
} else {
upperCharArray = Character.toChars(upperChar);
}
/* Grow result if needed */
int mapLen = upperCharArray.length;
if (mapLen > srcCount) {
char[] result2 = new char[result.length + mapLen - srcCount];
System.arraycopy(result, 0, result2, 0, i + resultOffset);
result = result2;
}
for (int x = 0; x < mapLen; ++x) {
result[i + resultOffset + x] = upperCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)upperChar;
}
}
return new String(result, 0, len + resultOffset);
}
/**
* toUpperCase(Locale locale)方法的重载,Local参数默认值为Locale.getDefault(),
* 默认的Local跟你当前JVM实例运行的主机系统平台环境有关
* @see java.lang.String#toUpperCase(Locale)
*/
public String toUpperCase() {
return toUpperCase(Locale.getDefault());
}
/**
* 剔除掉当前字符串对象的两头连续的空格字符,并返回一个新的字符串对象
*
* @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;
}
/**
* 因为当前字符串对象已经是String类型,因此这里直接return this.
*
* @return the string itself.
*/
public String toString() {
return this;
}
/**
* 将当前字符串对象转换成一个字符数组
*
* @return 返回当前字符串对象转换后得到的一个字符数组,该字符数组的长度与
* 当前字符串的长度一致
*/
public char[] toCharArray() {
// 由于类初始化问题,这里不能使用Arrays.copyOf()方法来实现
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}
/**
* 使用指定的字符串格式和参数返回一个格式化后的字符串
*
* @param format 字符串格式参数
*
* @param args 字符串格式中需要替换的参数
*
* //@throws IllegalFormatException
* 如果给定的字符串格式不合法或者给定的字符串格式与指定的参数不兼容,
* 或者给定的字符串格式需要的参数个数大于实际指定的参数个数,则会抛出
* IllegalFormatException异常
*
* @throws NullPointerException
* 如果format参数等于null,则会抛出NullPointerException异常
*
* @return 返回格式化后的字符串
*
* @see java.util.Formatter
* @since 1.5
*/
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
/**
* 使用指定的字符串格式和参数在指定的语言环境下返回一个格式化后的字符串
*
* @param l 本地语言环境
* @param format 字符串格式
* @param args 字符串格式中需要替换的参数
*
* //@throws IllegalFormatException
* 如果给定的字符串格式不合法或者给定的字符串格式与指定的参数不兼容,
* 或者给定的字符串格式需要的参数个数大于实际指定的参数个数,则会抛出
* IllegalFormatException异常
*
* @throws NullPointerException
* 如果format参数等于null,则会抛出NullPointerException异常
*
* @return 返回格式化后的字符串
*
* @see java.util.Formatter
* @since 1.5
*/
public static String format(Locale l, String format, Object... args) {
return new Formatter(l).format(format, args).toString();
}
/**
* 将指定的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);
}
/**
* 将指定的字符数组的[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);
}
/**
* 将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);
}
/**
* 返回字符串对象的规范化表示形式
* 字符串常量池初始化默认是空的,它由String类私下管理维护
* 当intern方法被执行,如果字符串常量池已经包含了与当前字符串对象equals的字符串,
* 那么直接从字符串常量池里返回该字符串对象即可,否则会将该字符串对象添加到常量池中
* 并返回.
* 因此对于任意的两个字符串s和t,
* 当且仅当s.equals(t) == true,
* s.intern() == t.intern()才会返回true.
* 所有的字符串字面量以及基于String的常量表达式都会被字符串常量池集中管理和返回.
*
* @return 从字符串常量池返回一个字符串,保持返回的字符串是在常量池中并且是唯一的
*/
public native String intern();
/** Hash计算需要的种子 */
private static final int HASHING_SEED;
static {
//当前的纳秒数
long nanos = System.nanoTime();
//当前的毫秒数
long now = System.currentTimeMillis();
//Hash种子利用了String类和System类的加载路径以及当前系统时间的纳秒数、毫秒数
int SEED_MATERIAL[] = {
System.identityHashCode(String.class),
System.identityHashCode(System.class),
(int) (nanos >>> 32),
(int) nanos,
(int) (now >>> 32),
(int) now,
(int) (System.nanoTime() >>> 2)
};
// 内部实现避免加载类
int h1 = 0;
// Hash种子计算的核心部分
for (int k1 : SEED_MATERIAL) {
k1 *= 0xcc9e2d51;
k1 = (k1 << 15) | (k1 >>> 17);
k1 *= 0x1b873593;
h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19);
h1 = h1 * 5 + 0xe6546b64;
}
// tail (always empty, as body is always 32-bit chunks)
// finalization
h1 ^= SEED_MATERIAL.length * 4;
// finalization mix force all bits of a hash block to avalanche
h1 ^= h1 >>> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >>> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >>> 16;
HASHING_SEED = h1;
}
/** 存储另一种Hash散列算法的计算结果,因为int占4个字节共32个bit,因此这里得到的是32位的Hash值 */
private transient int hash32 = 0;
/**
* 针对当前字符串对象计算得到一个32位的Hash值
*
* @return a 返回一个32位的Hash值
*/
int hash32() {
int h = hash32;
if (0 == h) {
// Murmur3 hashing函数针对碰撞攻击提供了优秀的保护机制
h = sun.misc.Hashing.murmur3_32(HASHING_SEED, value, 0, value.length);
// 确保最终计算的Hash值不为零,如果计算后得到为零,
// 强制赋值为1,以避免重复进行Hash计算
h = (0 != h) ? h : 1;
hash32 = h;
}
return h;
}
}