/**
* The {@code String} class represents character strings.
* 这个类是用来表示一个字符串
* All string literals in Java programs, such as {@code "abc"}, are implemented as instances of this class.
* 在Java程序中,所有的String字面量例如"abc",都作为该类的一个实例实现
*
* Strings are constant; their values cannot be changed after they
* are created. String buffers support mutable strings.
* String是一个常数,当他们被初始化之后,值就不能被改变。如果需要使用可变的字符串,可以使用String buffers
*
* Because String objects are immutable they can be shared. For example:
* String str = "abc";
* 因为String是不可变的,它们可以共享。下面代码中会共用同一个字符串对象,即str和"abc"引用的是同一个字符串
*
*
*
* is equivalent to:
* char data[] = {'a', 'b', 'c'};
* String str = new String(data);
* 上面这段代码这里虽然说是等价的,是对于字符串的值而言的,实际上使用了new 产生的str对象会在堆内存里面而不是从常量池中取。
* 这样很容易产生混淆,大部分的编码规范要求String的比较相等强制使用equals
*
* Here are some more examples of how strings can be used:
* <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} includes methods
* String类包含这些基本方法
* for examining individual characters of the sequence,
* 1、检查序列中的单个字符:例如,charAt(int index) 方法可以返回指定索引位置的字符
*
* for comparing strings,
* 2、比较字符串:例如,equals(Object anObject)方法,比较两个字符串是否相同
*
* for searching strings,
* 3、搜索字符串:例如,indexOf(int ch)和indexOf(String substring)方法可以在字符串中查找特定的字符或者子字符串位置
*
* for extracting substrings,
* 4、提取子字符串:例如,substring(int beginIndex)和substring(int beginIndex, int endIndex)方法可以从原始字符串中提取子字符串。
*
* and for creating a copy of a string with all characters translated to uppercase or to lowercase.
* 5、将字符串中的所有字符转换为大写或小写:例如,toUpperCase()和toLowerCase()方法可以将字符串中的所有字符转换为大写或小写。
*
* Case mapping is based on the Unicode Standard version
* specified by the {@link java.lang.Character Character} class.
* 其中,字符的大小写转换是基于Unicode标准进行的,具体的版本由Character类指定。
*
* <p>
* The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
* Java语言支持使用( + )进行符号拼接,上面" "在html中展示为空格
*
* String concatenation is implemented through the {@code StringBuilder}(or {@code StringBuffer}) class and its {@code append} method.
* String 支持使用 StringBuilder和StringBuffer两个Class的append()方法进行拼接;更推荐使用这两个类进行凭借,特别是使用循环处理String的时候
*
* String conversions are implemented through the method {@code toString}, defined by {@code Object} and inherited by all classes in Java.
* 转换String可以通过toString方法,这个方法定义在Object中并且被Java所有的类继承
*
* For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.
* 更多的信息可以参考《The Java Language Specification》
*
* 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.
* 除非另外指出,如果使用null来调用这个类的方法,将会抛出NullPointerException异常
*
* A {@code String} represents a string in the UTF-16 format in which
* <em>supplementary characters</em> are represented by <em>surrogate pairs</em>
* Java中String是使用UTF-16编码。关于编码的问题,后续在编码相关文字进行详细的探讨,这里要说明,Java使用UTF-16是一个历史遗留,虽然现在UTF-8更流行且更为兼容
*
* (see the section <a href="Character.html#unicode">Unicode Character Representations</a> in the {@code Character} class for more information).
* 可以通过查看Character类中关于Unicode字符表示的章节,以获取更多信息
* 当String使用补充字符的时候,charAt(int index)会获取道什么值呢?索引0会返回补充字符的高代理(high surrogate),而索引1会返回补充字符的低代理(low surrogate)
*
* Index values refer to {@code char} code units, so a supplementary character uses two positions in a {@code String}.
* index 索引只想的是char的代码单元,所以使用补充字符的时候,由于使用的是两个代码字符表示,所以他会占用两个索引位置
*
*
* The {@code String} 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} values).
* Java中的String类除了提供处理Unicode代码单元(即char值)的方法外,还提供了处理Unicode码点(即字符)的方法
* 例如上文所说的使用charAt(0)会返回补充字符的高代理(high surrogate),而使用codePointAt(0)会返回完整的码点
*
* @author Lee Boynton
* @author Arthur van Hoff
* @author Martin Buchholz
* @author Ulf Zibis
* @see java.lang.Object#toString() 所有的类都可以通过toString()方法转换为String,具体要看如何实现Object的toString()
* @see java.lang.StringBuffer 处理可变长度String的,一般用于循环内处理String,线程安全
* @see java.lang.StringBuilder 处理可变长度String的,一般用于循环内处理String,线程不安全
* @see java.nio.charset.Charset Java的核心类,表示字符集
* @since JDK1.0
*/
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/**
* The value is used for character storage.
* 用于存储字符串的value,按照推荐的数组定义风格可能会写成:
* char[] value;
*
*/
private final char value[];
/**
* Cache the hash code for the string
* 缓存String hash code,调用hashCode()方法时,如果hash的值不是0,则直接返回缓存值
*/
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>
*
* 这段代码定义了一个空的ObjectStreamField数组,意味着它不指定任何自定义的序列化字段。这意味着所有的默认字段都会被序列化。
*/
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
/**
* Initializes a newly created {@code String} object so that it represents
* an empty character sequence. Note that use of this constructor is
* unnecessary since Strings are immutable.
*
* 初始化一个""的新String对象,这个方法是不推荐使用的,因为String是不可变的
*/
public String() {
this.value = "".value;
}
/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
* 复制一个新的String对象,这个方法也是不推荐使用的,因为String是不可变的
*
* @param original
* A {@code String}
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
/**
* Allocates a new {@code String} so that it represents the sequence of
* characters currently contained in the character array argument. The
* contents of the character array are copied; subsequent modification of
* the character array does not affect the newly created string.
* 创建一个新的String对象,该对象表示字符数组中的字符序列
* 并且这个新创建的String对象与字符数组是独立的,修改字符数组不会影响这个String对象。
*
* @param value
* The initial value of the string
*/
public String(char value[]) {
// 这段代码可以看出,复制了一个新的数组,所有后面对字符数组的修改不会影响新创建的String对象。
this.value = Arrays.copyOf(value, value.length);
}
/**
* Allocates a new {@code String} that contains characters from a subarray
* of the character array argument. The {@code offset} argument is the
* index of the first character of the subarray and the {@code count}
* argument specifies the length of the subarray. The contents of the
* subarray are copied; subsequent modification of the character array does
* not affect the newly created string.
* 创建一个新的String对象,该对象表示字符数组中的一段字符序列
* 并且这个新创建的String对象与字符数组是独立的,修改字符数组不会影响这个String对象。
*
*
* @param value
* Array that is the source of characters
*
* @param offset
* The initial offset
*
* @param count
* The length
*
* @throws IndexOutOfBoundsException
* If the {@code offset} and {@code count} arguments index
* characters outside the bounds of the {@code value} array
*/
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.
/**
* 这里-1>>>1.是值Integer.MAX_VALUE,这一段注释的意思是,下面的if在逻辑上有两种写法
* 1、offset + count > value.length
* 2、offset > value.length - count
* 第一种很直观,一眼就看出偏移量不能大于值的长度,但是使用+可能导致int的溢出
* 使用 value.length - count能避免int的溢出
* 同样的,取两个数之间的中间值
* 方法一:mid = (l + r) / 2 : 容易溢出
* 方法二:mid = l + (r - l) / 2
*/
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
/**
* Allocates a new {@code String} that contains characters from a subarray
* of the <a href="Character.html#unicode">Unicode code point</a> array
* argument. The {@code offset} argument is the index of the first code
* point of the subarray and the {@code count} argument specifies the
* length of the subarray. The contents of the subarray are converted to
* {@code char}s; subsequent modification of the {@code int} array does not
* affect the newly created string.
* 通过Unicode码点数组生成String,不是很常用
*
* @param codePoints
* Array that is the source of Unicode code points
*
* @param offset
* The initial offset
*
* @param count
* The length
*
* @throws IllegalArgumentException
* If any invalid Unicode code point is found in {@code
* codePoints}
*
* @throws IndexOutOfBoundsException
* If the {@code offset} and {@code count} arguments index
* characters outside the bounds of the {@code codePoints} array
*
* @since 1.5
*/
public String(int[] codePoints, int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= codePoints.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > codePoints.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
final int end = offset + count;
// Pass 1: Compute precise size of char[]
// 1.计算字符长度,通常一个码点对应一个字符,但是扩展外的是由high surrogate和low surrogat组成的,所以要增加
int n = count;
for (int i = offset; i < end; i++) {
int c = codePoints[i];
if (Character.isBmpCodePoint(c))
continue;
else if (Character.isValidCodePoint(c))
// 扩展外的合法字符
n++;
// 存在不合法的字符,直接报错
else throw new IllegalArgumentException(Integer.toString(c));
}
// Pass 2: Allocate and fill in char[]
// 2.分配并填充一个数组
final char[] v = new char[n];
for (int i = offset, j = 0; i < end; i++, j++) {
int c = codePoints[i];
if (Character.isBmpCodePoint(c))
v[j] = (char)c;
else
// 处理扩展外字符
Character.toSurrogates(c, v, j++);
}
this.value = v;
}
/**
* Allocates a new {@code String} constructed from a subarray of an array
* of 8-bit integer values.
*
* <p> The {@code offset} argument is the index of the first byte of the
* subarray, and the {@code count} argument specifies the length of the
* subarray.
*
* <p> Each {@code byte} in the subarray is converted to a {@code char} as
* specified in the method above.
*
* @deprecated This method does not properly convert bytes into characters.
* As of JDK 1.1, the preferred way to do this is via the
* {@code String} constructors that take a {@link
* java.nio.charset.Charset}, charset name, or that use the platform's
* default charset.
*
* @param ascii
* The bytes to be converted to characters
*
* @param hibyte
* The top 8 bits of each 16-bit Unicode code unit
*
* @param offset
* The initial offset
* @param count
* The length
*
* @throws IndexOutOfBoundsException
* If the {@code offset} or {@code count} argument is invalid
*
* @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[])
* 已废弃的方法,主要是因为没有明确字符编码,可能导致不同的Java环境得到不同的结果
*/
@Deprecated
public String(byte ascii[], int hibyte, int offset, int count) {
checkBounds(ascii, offset, count);
char value[] = new char[count];
if (hibyte == 0) {
for (int i = count; i-- > 0;) {
value[i] = (char)(ascii[i + offset] & 0xff);
}
} else {
hibyte <<= 8;
for (int i = count; i-- > 0;) {
value[i] = (char)(hibyte | (ascii[i + offset] & 0xff));
}
}
this.value = value;
}
/**
* Allocates a new {@code String} containing characters constructed from
* an array of 8-bit integer values. Each character <i>c</i>in the
* resulting string is constructed from the corresponding component
* <i>b</i> in the byte array such that:
*
* <blockquote><pre>
* <b><i>c</i></b> == (char)(((hibyte & 0xff) << 8)
* | (<b><i>b</i></b> & 0xff))
* </pre></blockquote>
*
* @deprecated This method does not properly convert bytes into
* characters. As of JDK 1.1, the preferred way to do this is via the
* {@code String} constructors that take a {@link
* java.nio.charset.Charset}, charset name, or that use the platform's
* default charset.
*
* @param ascii
* The bytes to be converted to characters
*
* @param hibyte
* The top 8 bits of each 16-bit Unicode code unit
*
* @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[])
* 已废弃的方法,主要是因为没有明确字符编码,可能导致不同的Java环境得到不同的结果
*/
@Deprecated
public String(byte ascii[], int hibyte) {
this(ascii, hibyte, 0, ascii.length);
}
/* Common private utility method used to bounds check the byte array
* and requested offset & length values used by the String(byte[],..)
* constructors.
*/
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);
}
/**
* Constructs a new {@code String} by decoding the specified subarray of
* bytes using the specified charset. The length of the new {@code String}
* is a function of the charset, and hence may not be equal to the length
* of the subarray.
* 使用指定的一个字符集将byte数组转换为String
* 新创建的String的长度可能会因为字符集的不同而与原始字节子数组的长度不同。例如,在使用UTF-8编码时,一个字符可能由多个字节表示。
*
* <p> The behavior of this constructor when the given bytes are not valid
* in the given charset is unspecified. The {@link
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
* 如果给定的字节在指定的字符集中无效,这个构造方法的行为是不确定的。这意味着它可能不会抛出异常,但也可能产生不可预测的结果
* 如果需要更多的控制权来处理解码过程,应该使用java.nio.charset.CharsetDecoder类。这个类提供了更细致、更强大的字符解码功能。
*
* @param bytes 需要解码为字符的字节
* The bytes to be decoded into characters
*
* @param offset 要解码的第一个字节索引
* The index of the first byte to decode
*
* @param length 要解码的字节数
* The number of bytes to decode
* @param charsetName 使用的字符集
* The name of a supported {@linkplain java.nio.charset.Charset
* charset}
*
* @throws UnsupportedEncodingException
* If the named charset is not supported
*
* @throws IndexOutOfBoundsException
* If the {@code offset} and {@code length} arguments index
* characters outside the bounds of the {@code bytes} array
*
* @since JDK1.1
*/
public String(byte bytes[], int offset, int length, String charsetName)
throws UnsupportedEncodingException {
// 1.如果字符集为null,则抛出异常
if (charsetName == null)
throw new NullPointerException("charsetName");
// 2.检查字节数组、偏移量和长度是否有效,如果无效则抛出异常
checkBounds(bytes, offset, length);
// 3.调用了StringCoding.decode方法,使用指定的字符集名称、字节数组、偏移量和长度来解码字节,并将结果存储在实例变量value中
this.value = StringCoding.decode(charsetName, bytes, offset, length);
}
/**
* Constructs a new {@code String} by decoding the specified subarray of
* bytes using the specified {@linkplain java.nio.charset.Charset charset}.
* The length of the new {@code String} is a function of the charset, and
* hence may not be equal to the length of the subarray.
* 使用指定的一个字符集将byte数组转换为String
* 新创建的String的长度可能会因为字符集的不同而与原始字节子数组的长度不同。例如,在使用UTF-8编码时,一个字符可能由多个字节表示。
*
* <p> This method always replaces malformed-input and unmappable-character
* sequences with this charset's default replacement string. The {@link
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
* 当遇到格式不正确的输入或无法映射的字符序列时,该方法会使用该字符集的默认替换字符串来代替它们。
* 如果需要更精细地控制解码过程,应该使用java.nio.charset.CharsetDecoder类。
*
* @param bytes 需要解码为字符的字节
* The bytes to be decoded into characters
*
* @param offset 要解码的第一个字节索引
* The index of the first byte to decode
*
* @param length 要解码的字节数
* The number of bytes to decode
*
* @param charset 使用的字符集
* The {@linkplain java.nio.charset.Charset charset} to be used to
* decode the {@code bytes}
*
* @throws IndexOutOfBoundsException
* If the {@code offset} and {@code length} arguments index
* characters outside the bounds of the {@code bytes} array
*
* @since 1.6
*/
public String(byte bytes[], int offset, int length, Charset charset) {
// 1.如果传入的字符集对象是null,则抛出一个NullPointerException异常,异常信息为"charset"
if (charset == null)
throw new NullPointerException("charset");
// 2.检查字节数组、偏移量和长度是否有效,如果无效则抛出异常
checkBounds(bytes, offset, length);
// 3.调用了StringCoding.decode方法,使用指定的字符集名称、字节数组、偏移量和长度来解码字节,并将结果存储在实例变量value中
this.value = StringCoding.decode(charset, bytes, offset, length);
}
/**
* Constructs a new {@code String} by decoding the specified array of bytes
* using the specified {@linkplain java.nio.charset.Charset charset}. The
* length of the new {@code String} is a function of the charset, and hence
* may not be equal to the length of the byte array.
* 使用指定的一个字符集将byte数组转换为String
* 新创建的String的长度可能会因为字符集的不同而与原始字节子数组的长度不同。例如,在使用UTF-8编码时,一个字符可能由多个字节表示。
*
* <p> The behavior of this constructor when the given bytes are not valid
* in the given charset is unspecified. The {@link
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
* 如果给定的字节在指定的字符集中无效,这个构造方法的行为是不确定的。这意味着它可能不会抛出异常,但也可能产生不可预测的结果
* 如果需要更多的控制权来处理解码过程,应该使用java.nio.charset.CharsetDecoder类。这个类提供了更细致、更强大的字符解码功能。
*
* @param bytes
* The bytes to be decoded into characters
*
* @param charsetName
* The name of a supported {@linkplain java.nio.charset.Charset
* charset}
*
* @throws UnsupportedEncodingException
* If the named charset is not supported
*
* @since JDK1.1
*/
public String(byte bytes[], String charsetName)
throws UnsupportedEncodingException {
this(bytes, 0, bytes.length, charsetName);
}
/**
* Constructs a new {@code String} by decoding the specified array of
* bytes using the specified {@linkplain java.nio.charset.Charset charset}.
* The length of the new {@code String} is a function of the charset, and
* hence may not be equal to the length of the byte array.
* 使用指定的一个字符集将byte数组转换为String
* 新创建的String的长度可能会因为字符集的不同而与原始字节子数组的长度不同。例如,在使用UTF-8编码时,一个字符可能由多个字节表示。
*
* <p> This method always replaces malformed-input and unmappable-character
* sequences with this charset's default replacement string. The {@link
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
* 当遇到格式不正确的输入或无法映射的字符序列时,该方法会使用该字符集的默认替换字符串来代替它们。
* 如果需要更精细地控制解码过程,应该使用java.nio.charset.CharsetDecoder类。
*
* @param bytes
* The bytes to be decoded into characters
*
* @param charset
* The {@linkplain java.nio.charset.Charset charset} to be used to
* decode the {@code bytes}
*
* @since 1.6
*/
public String(byte bytes[], Charset charset) {
this(bytes, 0, bytes.length, charset);
}
/**
* Constructs a new {@code String} by decoding the specified subarray of
* bytes using the platform's default charset. The length of the new
* {@code String} is a function of the charset, and hence may not be equal
* to the length of the subarray.
* 用于创建一个新的String对象,该对象表示由给定的字节数组下解码得到的字符序列。
* 解码过程中使用的是平台默认的字符集
*
* <p> The behavior of this constructor when the given bytes are not valid
* in the default charset is unspecified. The {@link
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
*
* @param bytes
* The bytes to be decoded into characters
*
* @param offset
* The index of the first byte to decode
*
* @param length
* The number of bytes to decode
*
* @throws IndexOutOfBoundsException
* If the {@code offset} and the {@code length} arguments index
* characters outside the bounds of the {@code bytes} array
*
* @since JDK1.1
*/
public String(byte bytes[], int offset, int length) {
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(bytes, offset, length);
}
/**
* Constructs a new {@code String} by decoding the specified array of bytes
* using the platform's default charset. The length of the new {@code
* String} is a function of the charset, and hence may not be equal to the
* length of the byte array.
* 用于创建一个新的String对象,该对象表示由给定的字节数组下解码得到的字符序列。
* 解码过程中使用的是平台默认的字符集
*
* <p> The behavior of this constructor when the given bytes are not valid
* in the default charset is unspecified. The {@link
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
*
* @param bytes
* The bytes to be decoded into characters
*
* @since JDK1.1
*/
public String(byte bytes[]) {
this(bytes, 0, bytes.length);
}
/**
* Allocates a new string that contains the sequence of characters
* currently contained in the string buffer argument. The contents of the
* string buffer are copied; subsequent modification of the string buffer
* does not affect the newly created string.
* 从一个StringBuffer对象创建一个新的String对象
* 这个构造方法将StringBuffer的内容复制到一个新的字符串中,这样对StringBuffer的后续修改不会影响已创建的字符串。
*
* @param buffer
* A {@code StringBuffer}
*/
public String(StringBuffer buffer) {
// synchronized 保证buffer线程安全
synchronized(buffer) {
// 使用Arrays.copyOf方法复制了StringBuffer的内容到一个新的字符数组
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
}
/**
* Allocates a new string that contains the sequence of characters
* currently contained in the string builder argument. The contents of the
* string builder are copied; subsequent modification of the string builder
* does not affect the newly created string.
* 从一个StringBuilder对象创建一个新的String对象。这个构造方法将StringBuilder的内容复制到一个新的字符串中,这样对StringBuilder的后续修改不会影响已创建的字符串。
*
* <p> This constructor is provided to ease migration to {@code
* StringBuilder}. Obtaining a string from a string builder via the {@code
* toString} method is likely to run faster and is generally preferred.
* 在将StringBuilder转换为Stringd的方式中,使用toString()方法更推荐
*
* @param builder
* A {@code StringBuilder}
*
* @since 1.5
*/
public String(StringBuilder builder) {
// 使用Arrays.copyOf方法复制了StringBuilder的内容到一个新的字符数组
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
/**
* Package private constructor which shares value array for speed.
* this constructor is always expected to be called with share==true.
* a separate constructor is needed because we already have a public
* String(char[]) constructor that makes a copy of the given char[].
* 包私有的方法,创建一个数组共享的String;
* 不常用,在目前不支持 unshared
*
*/
String(char[] value, boolean share) {
// assert share : "unshared not supported";
this.value = value;
}
/**
* Returns the length of this string.
* 返回字符串的长度
*
* The length is equal to the number of <a href="Character.html#unicode">Unicode
* code units</a> in the string.
* 字符串的长度等于其中的Unicode码位的数量
*
* @return the length of the sequence of characters represented by this
* object.
*/
public int length() {
return value.length;
}
/**
* Returns {@code true} if, and only if, {@link #length()} is {@code 0}.
* 当且仅当字符串长度为0的时候,返回ture。
* 一般来说这个方法不常用,更常用的时使用lang3包的StringUtils.isEmpty()方法
*
* @return {@code true} if {@link #length()} is {@code 0}, otherwise
* {@code false}
*
* @since 1.6
*/
public boolean isEmpty() {
return value.length == 0;
}
/**
* Returns the {@code char} value at the specified index.
* 返回索引位置的value
*
* An index ranges from {@code 0} to {@code length() - 1}.
* 索引的范围从0到length()-1
*
* The first {@code char} value of the sequence
* is at index {@code 0}, the next at index {@code 1},
* and so on, as for array indexing.
*
* If the {@code char} value specified by the index is a
* <a href="Character.html#unicode">surrogate</a>, the surrogate
* value is returned.
* 如果索引位置是代理值,则会直接返回代理值
*
* @param index the index of the {@code char} value.
* @return the {@code char} value at the specified index of this string.
* The first {@code char} value is at index {@code 0}.
* @exception IndexOutOfBoundsException if the {@code index}
* argument is negative or not less than the length of this
* string.
*/
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
/**
* Returns the character (Unicode code point) at the specified index.
* 返回该索引位置上的字符以及其对应的Unicode代码点
* The index refers to {@code char} values (Unicode code units) and ranges from {@code 0} to {@link #length()}{@code - 1}.
* 索引的范围从0到length()-1
*
* If the {@code char} value specified at the given index is in the high-surrogate range,
* 如果索引处的字符是是high-surrogate一部分
* the following index is less than the length of this {@code String},
* 下一个索引在字符串的范围内
* and the {@code char} value at the following index is in the low-surrogate range,
* 并且下一个索引是low-surrogate范围
* then the supplementary code point corresponding to this surrogate pair is returned.
* 如果满足条件,那么返回的是代理对对应的补充代码对
* Otherwise, the {@code char} value at the given index is returned.
* 如果给定的字符不是代理对的一部分,那么直接返回该索引出的字符
*
* @param index the index to the {@code char} values
* @return the code point value of the character at the
* {@code index}
* @exception IndexOutOfBoundsException if the {@code index}
* argument is negative or not less than the length of this
* string.
* @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);
}
/**
* Returns the character (Unicode code point) before the specified index.
* 返回该索引位置前的字符以及其对应的Unicode代码点
*
* The index refers to {@code char} values (Unicode code units) and ranges from {@code 1} to {@link CharSequence#length() length}.
* 索引的范围是[1, length]
*
* <p> If the {@code char} value at {@code (index - 1)} is in the low-surrogate range, {@code (index - 2)} is not negative,
* 如果index - 1位置 low-surrogate,且 index - 2 不是负数
* and the {@code char} value at {@code (index - 2)} is in the high-surrogate range,
* index - 2的位置是 high -surrogate
* then the supplementary code point value of the surrogate pair is returned.
* 如果满足条件,那么返回的是代理对对应的补充代码对
*
* If the {@code char} value at {@code index - 1} is an unpaired low-surrogate or a high-surrogate, the surrogate value is returned.
* 如果index - 1 是一个不匹配的low-surrogate或者是一个 high-surrgate,则会返回代理字符本身
*
* @param index the index following the code point that should be returned
* @return the Unicode code point value before the given index.
* @exception IndexOutOfBoundsException if the {@code index}
* argument is less than 1 or greater than the length
* of this string.
* @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);
}
/**
* Returns the number of Unicode code points in the specified text range of this {@code String}.
* 返回Unicode代码点数量
*
* The text range begins at the specified {@code beginIndex} and extends to the {@code char} at index {@code endIndex - 1}.
* 文本的范围是,从beginIndex开始,到endIndex - 1
*
* Thus the length (in {@code char}s) of the text range is {@code endIndex-beginIndex}.
* 长度是范围是 endIndex - beginIndex
*
* Unpaired surrogates within the text range count as one code point each.
* 未匹配的代理,将会作为一个代码点
*
* @param beginIndex the index to the first {@code char} of
* the text range.
* @param endIndex the index after the last {@code char} of
* the text range.
* @return the number of Unicode code points in the specified text
* range
* @exception IndexOutOfBoundsException if the
* {@code beginIndex} is negative, or {@code endIndex}
* is larger than the length of this {@code String}, or
* {@code beginIndex} is larger than {@code 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);
}
/**
* Returns the index within this {@code String} that is offset from the given {@code index} by {@code codePointOffset} code points.
* 计算给定索引位置的字符在字符串中的偏移量
*
* Unpaired surrogates within the text range given by {@code index} and {@code codePointOffset} count as one code point each.
* 在给定的文本范围内,未配对的代理字符(surrogates)每个都算作一个代码点
*
* @param index the index to be offset
* @param codePointOffset the offset in code points
* @return the index within this {@code String}
* @exception IndexOutOfBoundsException if {@code index}
* is negative or larger then the length of this
* {@code String}, or if {@code codePointOffset} is positive
* and the substring starting with {@code index} has fewer
* than {@code codePointOffset} code points,
* or if {@code codePointOffset} is negative and the substring
* before {@code index} has fewer than the absolute value
* of {@code codePointOffset} code points.
* @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);
}
/**
* Copy characters from this string into dst starting at dstBegin.
* 将此字符串中的字符从dstBegin开始复制到dst中。
*
* This method doesn't perform any range checking.
* 这个方法不会做范围检查
*/
void getChars(char dst[], int dstBegin) {
System.arraycopy(value, 0, dst, dstBegin, value.length);
}
/**
* Copies characters from this string into the destination character array.
* 将字符串中的一部分字符复制到目标字符数组中
*
* The first character to be copied is at index {@code srcBegin};
* 源字符串中要开始复制的索引位置
*
* the last character to be copied is at index {@code srcEnd-1}
* 源字符串中要结束复制的索引位置(不包括该位置的字符
*
* (thus the total number of characters to be copied is {@code srcEnd-srcBegin}).
* 要复制的总数为srcEnd-srcBegin
*
* The characters are copied into the subarray of {@code dst} starting at index {@code dstBegin}
* 目标数组中开始存储字符的索引位置
*
* and ending at index:
* <blockquote><pre>
* dstBegin + (srcEnd-srcBegin) - 1
* </pre></blockquote>
* 复制到 dstBegin + (srcEnd-srcBegin) - 1 索引位置
*
* @param srcBegin index of the first character in the string
* to copy.
* @param srcEnd index after the last character in the string
* to copy.
* @param dst the destination array.
* @param dstBegin the start offset in the destination array.
* @exception IndexOutOfBoundsException If any of the following
* is true:
* <ul><li>{@code srcBegin} is negative.
* <li>{@code srcBegin} is greater than {@code srcEnd}
* <li>{@code srcEnd} is greater than the length of this
* string
* <li>{@code dstBegin} is negative
* <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than
* {@code dst.length}</ul>
*/
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);
}
/**
* Copies characters from this string into the destination byte array. Each
* byte receives the 8 low-order bits of the corresponding character. The
* eight high-order bits of each character are not copied and do not
* participate in the transfer in any way.
*
* <p> The first character to be copied is at index {@code srcBegin}; the
* last character to be copied is at index {@code srcEnd-1}. The total
* number of characters to be copied is {@code srcEnd-srcBegin}. The
* characters, converted to bytes, are copied into the subarray of {@code
* dst} starting at index {@code dstBegin} and ending at index:
*
* <blockquote><pre>
* dstBegin + (srcEnd-srcBegin) - 1
* </pre></blockquote>
*
* @deprecated This method does not properly convert characters into
* bytes. As of JDK 1.1, the preferred way to do this is via the
* {@link #getBytes()} method, which uses the platform's default charset.
*
* @param srcBegin
* Index of the first character in the string to copy
*
* @param srcEnd
* Index after the last character in the string to copy
*
* @param dst
* The destination array
*
* @param dstBegin
* The start offset in the destination array
*
* @throws IndexOutOfBoundsException
* If any of the following is true:
* <ul>
* <li> {@code srcBegin} is negative
* <li> {@code srcBegin} is greater than {@code srcEnd}
* <li> {@code srcEnd} is greater than the length of this String
* <li> {@code dstBegin} is negative
* <li> {@code dstBegin+(srcEnd-srcBegin)} is larger than {@code
* dst.length}
* </ul>
*/
@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);
}
Objects.requireNonNull(dst);
int j = dstBegin;
int n = srcEnd;
int i = srcBegin;
char[] val = value; /* avoid getfield opcode */
while (i < n) {
dst[j++] = (byte)val[i++];
}
}
/**
* Encodes this {@code String} into a sequence of bytes using the named
* charset, storing the result into a new byte array.
*
* <p> The behavior of this method when this string cannot be encoded in
* the given charset is unspecified. The {@link
* java.nio.charset.CharsetEncoder} class should be used when more control
* over the encoding process is required.
*
* @param charsetName
* The name of a supported {@linkplain java.nio.charset.Charset
* charset}
*
* @return The resultant byte array
*
* @throws UnsupportedEncodingException
* If the named charset is not supported
*
* @since JDK1.1
*/
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException {
if (charsetName == null) throw new NullPointerException();
return StringCoding.encode(charsetName, value, 0, value.length);
}
/**
* Encodes this {@code String} into a sequence of bytes using the given
* {@linkplain java.nio.charset.Charset charset}, storing the result into a
* new byte array.
*
* <p> This method always replaces malformed-input and unmappable-character
* sequences with this charset's default replacement byte array. The
* {@link java.nio.charset.CharsetEncoder} class should be used when more
* control over the encoding process is required.
*
* @param charset
* The {@linkplain java.nio.charset.Charset} to be used to encode
* the {@code String}
*
* @return The resultant byte array
*
* @since 1.6
*/
public byte[] getBytes(Charset charset) {
if (charset == null) throw new NullPointerException();
return StringCoding.encode(charset, value, 0, value.length);
}
/**
* Encodes this {@code String} into a sequence of bytes using the
* platform's default charset, storing the result into a new byte array.
*
* <p> The behavior of this method when this string cannot be encoded in
* the default charset is unspecified. The {@link
* java.nio.charset.CharsetEncoder} class should be used when more control
* over the encoding process is required.
*
* @return The resultant byte array
*
* @since JDK1.1
*/
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}
/**
* Compares this string to the specified object.
* 比较字符串
*
* The result is {@code true} if and only if
* the argument is not {@code null}
* and is a {@code String} object that represents the same sequence of characters as this object.
* 当且仅当参数非空且字符串子序列相等时返回true
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
// 判断长度
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
// 循环对比字符串
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
/**
* Compares this string to the specified {@code StringBuffer}.
* 比较String和StringBUffer的值是否相同
*
* The result is {@code true} if and only if this {@code String} represents the same sequence of characters as the specified {@code StringBuffer}.
* 注释中提到了true如果且仅如果这个String表示与指定StringBuffer相同的字符序列。
*
* This method synchronizes on the {@code StringBuffer}.
* 方法会同步于StringBuffer对象。这意味着在进行比较操作时,可能会对StringBuffer对象进行同步处理,确保其线程安全。
*
* @param sb
* The {@code StringBuffer} to compare this {@code String} against
*
* @return {@code true} if this {@code String} represents the same
* sequence of characters as the specified {@code StringBuffer},
* {@code false} otherwise
*
* @since 1.4
*/
public boolean contentEquals(StringBuffer sb) {
return contentEquals((CharSequence)sb);
}
/**
* 非同步的内容比较,AbstractStringBuilder两个实现是StringBuffer和StringBuilder
*/
private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
char v1[] = value;
char v2[] = sb.getValue();
int n = v1.length;
if (n != sb.length()) {
return false;
}
for (int i = 0; i < n; i++) {
if (v1[i] != v2[i]) {
return false;
}
}
return true;
}
/**
* Compares this string to the specified {@code CharSequence}.
* 比较字符串
*
* The result is {@code true} if and only if this {@code String} represents the
* same sequence of char values as the specified sequence.
* 比较字符序列的内容是否相同
*
* Note that if the {@code CharSequence} is a {@code StringBuffer} then the method synchronizes on it.
* 如果CharSequence是一个StringBuffer,那么该方法会对其同步
*
* @param cs
* The sequence to compare this {@code String} against
*
* @return {@code true} if this {@code String} represents the same
* sequence of char values as the specified sequence, {@code
* false} otherwise
*
* @since 1.5
*/
public boolean contentEquals(CharSequence cs) {
// Argument is a StringBuffer, StringBuilder
// 如果参数是StringBuffer或者StringBuilder
if (cs instanceof AbstractStringBuilder) {
if (cs instanceof StringBuffer) {
// StringBuffer是线程安全的
synchronized(cs) {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
} else {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
}
// Argument is a String
// 类型是String,则直接调用equals方法
if (cs instanceof String) {
return equals(cs);
}
// Argument is a generic CharSequence
// 如果参数是一般的CharSequence,则逐一比较char
char v1[] = value;
int n = v1.length;
if (n != cs.length()) {
return false;
}
for (int i = 0; i < n; i++) {
if (v1[i] != cs.charAt(i)) {
return false;
}
}
return true;
}
/**
* Compares this {@code String} to another {@code String}, ignoring case considerations.
* 比较字符串忽略大消息
*
* Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings
* are equal ignoring case.
*
* <p> Two characters {@code c1} and {@code c2} are considered the same
* ignoring case if at least one of the following is true:
* <ul>
* <li> The two characters are the same (as compared by the
* {@code ==} operator)
* <li> Applying the method {@link
* java.lang.Character#toUpperCase(char)} to each character
* produces the same result
* <li> Applying the method {@link
* java.lang.Character#toLowerCase(char)} to each character
* produces the same result
* </ul>
*
* @param anotherString
* The {@code String} to compare this {@code String} against
*
* @return {@code true} if the argument is not {@code null} and it
* represents an equivalent {@code String} ignoring case; {@code
* false} otherwise
*
* @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);
}
/**
* Compares two strings lexicographically.
* 比较两个字符串
*
* The comparison is based on the Unicode value of each character in the strings.
* The character sequence represented by this {@code String} object is compared lexicographically to the
* character sequence represented by the argument string.
* 对字符串的每个字符的字典值进行比较
*
* The result is a negative integer if this {@code String} object lexicographically precedes the argument string.
* 如果String在字典中位于参数字符串之前,则返回负整数
*
* The result is a positive integer if this {@code String} object lexicographically follows the argument string.
* 如果String在字典中位于参数字符串之后,则返回正整数
*
* The result is zero if the strings are equal; {@code compareTo} returns {@code 0} exactly when
* the {@link #equals(Object)} method would return {@code true}.
* 如果string相等,则返回true,而且equals也会返回true
*
* This is the definition of lexicographic ordering.
* If two strings are different, then either they have different characters at some index
* that is a valid index for both strings, or their lengths are different, or both.
* 如果两个字符串不同,则他们在某个索引处的字符会不同,或者长度会不同,或者两种都有
*
* If they have different characters at one or more index positions, let <i>k</i> be the smallest such index;
* 如果它们在一个或多个索引位置有不同的字符,找到最小的索引位置
*
* then the string whose character at position <i>k</i> has the smaller value, as determined by using the <
* 比较这两个字符串在k索引位置上的字符,根据<运算符来确定哪个字符的值更小
*
* operator, lexicographically precedes the other string.
* 根据这个比较结果,较小的字符对应的字符串在字典序上排在前面
*
* In this case, {@code compareTo} returns the difference of the two character values at position {@code k} in the two string -- that is, the value:
* 在这种情况下将会返回两个字符串中k索引出的两个字符的差
* this.charAt(k)-anotherString.charAt(k)
*
* If there is no index position at which they differ, then the shorterstring lexicographically precedes the longer string.
* 如果没有不存在不同的索引位置,则较短的字符先于较长的字符
* In this case, {@code compareTo} returns the difference of the lengths of the strings -- that is, the value:
* 在这种情况下,将返回字符串的长度差
* this.length()-anotherString.length()
*
* @param anotherString the {@code String} to be compared.
* @return the value {@code 0} if the argument string is equal to
* this string; a value less than {@code 0} if this string
* is lexicographically less than the string argument; and a
* value greater than {@code 0} if this string is
* lexicographically greater than the string argument.
*/
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
// 如果存在字符不同的索引,则返回字符的差
return c1 - c2;
}
k++;
}
// 如果不存在字符不同的索引,则返回字符串的长度差
return len1 - len2;
}
/**
* A Comparator that orders {@code String} objects as by {@code compareToIgnoreCase}.
* String的比较器进行忽略大小写的比较
* This comparator is serializable.
*
* Note that this Comparator does <em>not</em> take locale into account,
* and will result in an unsatisfactory ordering for certain locales.
* 比较器没有考虑区域的设置,可能会导致不理想的结果
*
* The java.text package provides <em>Collators</em> to allow
* locale-sensitive ordering.
* 如果需要更复杂的、考虑地区的字符串比较推荐使用java.text.Collator#compare(String, String)
*
* @see java.text.Collator#compare(String, String)
* @since 1.2
*/
public static final Comparator<String> CASE_INSENSITIVE_ORDER
= new CaseInsensitiveComparator();
/**
* 私有静态内部类,该类实现了Comparator<String>接口和java.io.Serializable接口。这个比较器用于比较两个字符串,而不考虑它们的大小写
*/
private static class CaseInsensitiveComparator
implements Comparator<String>, java.io.Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
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) {
// No overflow because of numeric promotion
return c1 - c2;
}
}
}
}
return n1 - n2;
}
/** Replaces the de-serialized object. */
private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
}
/**
* Compares two strings lexicographically, ignoring case differences.
* 比较两个字符串,忽略大小写
*
* This method returns an integer whose sign is that of calling {@code compareTo} with normalized versions of the strings
* where case differences have been eliminated by calling {@code Character.toLowerCase(Character.toUpperCase(character))} on each character.
* 这个方法返回的整数,逻辑和compareTo一样,详情看compareTo方法的分析
*
* Note that this method does <em>not</em> take locale into account,
* and will result in an unsatisfactory ordering for certain locales.
* The java.text package provides <em>collators</em> to allow
* locale-sensitive ordering.
* 此方法不考虑区域设置
*
* @param str the {@code String} to be compared.
* @return a negative integer, zero, or a positive integer as the
* specified String is greater than, equal to, or less
* than this String, ignoring case considerations.
* @see java.text.Collator#compare(String, String)
* @since 1.2
*/
public int compareToIgnoreCase(String str) {
return CASE_INSENSITIVE_ORDER.compare(this, str);
}
/**
* Tests if two string regions are equal.
* 区域匹配
*
* 满足其他条件是时,返回false:
* 1、toffset为负数
* 2、ooffset为负数
* 3、toffset+len 大于 this的长度
* 4、ooffset+len 大于 other的长度
* 5、如果上面条件都不满足,则存在不相等的字符
*
* @param toffset this strings的开始偏移量
* @param other 比较的字符串.
* @param ooffset 比较字符串的偏移量
* @param len 需要比较的长度
* @return {@code true} if the specified subregion of this string
* exactly matches the specified subregion of the string argument;
* {@code false} otherwise.
*/
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;
}
/**
* Tests if two string regions are equal.
* 重载了上面regionMatches方法,增加了是否忽略大小写判断
*
* @param ignoreCase if {@code true}, ignore case when comparing
* characters.
* @param toffset the starting offset of the subregion in this
* string.
* @param other the string argument.
* @param ooffset the starting offset of the subregion in the string
* argument.
* @param len the number of characters to compare.
* @return {@code true} if the specified subregion of this string
* matches the specified subregion of the string argument;
* {@code false} otherwise. Whether the matching is exact
* or case insensitive depends on the {@code ignoreCase}
* argument.
*/
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;
}
// 字符串转换为大写并不适用于格鲁吉亚字母表,因为格鲁吉亚字母表在大小写转换方面有特殊的规则。因此,在退出之前,我们需要进行最后一次检查。
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}
}
return false;
}
return true;
}
/**
* 测试字符串是由从指定索引位置开始,已指定的前缀开头
*
* @param prefix 匹配的前缀
* @param toffset 开始匹配的索引
* @return 如果传递的参数是从toffset索引开始的子字符串的前缀,则返回true;否则返回false
* 如果toffset时负数或者大于String的长度,则返回false;否则与this.substring(toffset).startsWith(prefix)时等价的
*/
public boolean startsWith(String prefix, int toffset) {
char ta[] = value;
int to = toffset;
char pa[] = prefix.value;
int po = 0;
int pc = prefix.value.length;
// Note: toffset might be near -1>>>1.
if ((toffset < 0) || (toffset > value.length - pc)) {
return false;
}
// 逐个字符进行比较
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
return true;
}
/**
* Tests if this string starts with the specified prefix.
* 判断字符串已指定的前缀开头
*
* @param prefix 前缀
* @return {@code true} if the character sequence represented by the
* argument is a prefix of the character sequence represented by
* this string; {@code false} otherwise.
* Note also that {@code true} will be returned if the
* argument is an empty string or is equal to this
* {@code String} object as determined by the
* {@link #equals(Object)} method.
* @since 1. 0
*/
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
/**
* Tests if this string ends with the specified suffix.
* 判断字符串已指定的后缀结尾
*
* @param suffix the suffix.
* @return {@code true} if the character sequence represented by the
* argument is a suffix of the character sequence represented by
* this object; {@code false} otherwise. Note that the
* result will be {@code true} if the argument is the
* empty string or is equal to this {@code String} object
* as determined by the {@link #equals(Object)} method.
*/
public boolean endsWith(String suffix) {
// 比较string最后从suffix同长度的索引的子字符串是否相等
return startsWith(suffix, value.length - suffix.value.length);
}
/**
* Returns a hash code for this string.
* 返回这个String的hash code
*
* hash code的计算方式: s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* s[i]:表示字符串中的第i个字符
* n:表示字符串的长度
* ^:表示指数运算
* 空字符串的hash code为0
* @return a hash code value for this object.
*/
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;
}
/**
* Returns the index within this string of the first occurrence of the specified character.
* 返回指定字符第一次出现的索引
* 该字符存在于字符串中,则返回其位置;否则返回-1
*
* @param ch a character (Unicode code point).
* @return the index of the first occurrence of the character in the
* character sequence represented by this object, or
* {@code -1} if the character does not occur.
*/
public int indexOf(int ch) {
return indexOf(ch, 0);
}
/**
* 从指定索引处开始匹配,返回第一次出现字符的索引位置
* 该字符存在于字符串中,则返回其位置;否则返回-1
* 该方法适用于处理包括各种特殊字符在内的所有Unicode字符
*
* @param ch a character (Unicode code point).
* @param fromIndex the index to start the search from.
* @return the index of the first occurrence of the character in the
* character sequence represented by this object that is greater
* than or equal to {@code fromIndex}, or {@code -1}
* if the character does not occur.
*/
public int indexOf(int ch, int fromIndex) {
final int max = value.length;
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= max) {
// Note: fromIndex might be near -1>>>1.
return -1;
}
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// 这个常量是Unicode中补充码位的最低值。如果ch小于这个值,说明它是一个BMP码位(基础多语言平面码位)或者是一个负值(无效码位)
// handle most cases here (ch is a BMP code point or a
// negative value (invalid code point))
final char[] value = this.value;
for (int i = fromIndex; i < max; i++) {
if (value[i] == ch) {
return i;
}
}
return -1;
} else {
// 如果ch不小于Character.MIN_SUPPLEMENTARY_CODE_POINT,那么调用indexOfSupplementary(ch, fromIndex)方法来查找字符的位置。
return indexOfSupplementary(ch, fromIndex);
}
}
/**
* Handles (rare) calls of indexOf with a supplementary character.
* 处理使用补充码位的字符的indexOf方法的调用
*/
private int indexOfSupplementary(int ch, int fromIndex) {
if (Character.isValidCodePoint(ch)) {
final char[] value = this.value;
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;
}
/**
* 获取最后一个匹配的值
*
* @param ch a character (Unicode code point).
* @return the index of the last occurrence of the character in the
* character sequence represented by this object, or
* {@code -1} if the character does not occur.
*/
public int lastIndexOf(int ch) {
return lastIndexOf(ch, value.length - 1);
}
/**
* 返回从索引位置往前最后一个匹配的值
*/
public int lastIndexOf(int ch, int fromIndex) {
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// 这个常量是Unicode中补充码位的最低值。如果ch小于这个值,说明它是一个BMP码位(基础多语言平面码位)或者是一个负值(无效码位)
// handle most cases here (ch is a BMP code point or a
// negative value (invalid 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);
}
}
/**
* Handles (rare) calls of lastIndexOf with a supplementary character.
* 处理使用补充码位的字符的lastIndexOf方法的调用
*/
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;
}
/**
* 返回第一次出现字符串的索引位置
*
* @param str the substring to search for.
* @return the index of the first occurrence of the specified substring,
* or {@code -1} if there is no such occurrence.
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
/**
* 从指定索引处开始匹配,返回第一次出现字符串的索引位置
*
* @param str the substring to search for.
* @param fromIndex the index from which to start the search.
* @return the index of the first occurrence of the specified substring,
* starting at the specified index,
* or {@code -1} if there is no such occurrence.
*/
public int indexOf(String str, int fromIndex) {
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/**
* 在字符数组source中查找字符串target的首次出现位置。这个方法可能是String类和AbstractStringBuilder类共享的代码的一部分
*
* @param source the characters being searched.字符数组,即被搜索的字符串
* @param sourceOffset offset of the source string.源字符串的起始偏移量。
* @param sourceCount count of the source string.源字符串的长度。
* @param target the characters being searched for.要搜索的字符串。
* @param fromIndex the index to begin searching from.开始搜索的位置。
*/
static int indexOf(char[] source, int sourceOffset, int sourceCount,
String target, int fromIndex) {
return indexOf(source, sourceOffset, sourceCount,
target.value, 0, target.value.length,
fromIndex);
}
/**
* 描述了String和StringBuffer类共享的用于执行搜索操作的代码。源是正在被搜索的字符数组,目标是正在被搜索的字符串。
*
* @param source the characters being searched.字符数组,即被搜索的字符串
* @param sourceOffset offset of the source string.源字符串的起始偏移量。
* @param sourceCount count of the source string.源字符串的长度。
* @param target the characters being searched for.要搜索的字符串。
* @param targetOffset offset of the target string.搜索的字符串的起始偏移量。
* @param targetCount count of the target string.搜索的字符串的长度。
* @param fromIndex the index to begin searching from.开始搜索的位置。
*/
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++) {
/* Look for first character. */
// 寻找第一个字符
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* Found first character, now look at the rest of v2 */
// 找到第一个字符后往后比较剩余的字符
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) {
/* Found whole string. */
// 如果找到子串,则返回子串在源字符串中的起始位置(相对于源字符串的偏移量)
return i - sourceOffset;
}
}
}
return -1;
}
/**
* 最后一次出现字符串的位置
*
* @param str the substring to search for.
* @return the index of the last occurrence of the specified substring,
* or {@code -1} if there is no such occurrence.
*/
public int lastIndexOf(String str) {
return lastIndexOf(str, value.length);
}
/**
* 最后一次出现字符串的位置,从索引位置往前
*
* @param str the substring to search for.
* @param fromIndex
* @return the index of the last occurrence of the specified substring,
* searching backward from the specified index,
* or {@code -1} if there is no such occurrence.
*/
public int lastIndexOf(String str, int fromIndex) {
return lastIndexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/**
* Code shared by String and AbstractStringBuilder to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* @param source the characters being searched.源字符数组,即被搜索的字符串。
* @param sourceOffset offset of the source string.源字符串的起始偏移量。
* @param sourceCount count of the source string.源字符串的长度。
* @param target the characters being searched for.要搜索的子串字符数组。
* @param fromIndex the index to begin searching from.开始搜索的位置。
*/
static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
String target, int fromIndex) {
return lastIndexOf(source, sourceOffset, sourceCount,
target.value, 0, target.value.length,
fromIndex);
}
/**
* Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* @param source the characters being searched.源字符数组,即被搜索的字符串。
* @param sourceOffset offset of the source string.源字符串的起始偏移量。
* @param sourceCount count of the source string.源字符串的长度。
* @param target the characters being searched for.要搜索的子串字符数组
* @param targetOffset offset of the target string.子串字符数组的起始偏移量
* @param targetCount count of the target string.子串的长度
* @param fromIndex the index to begin searching from.
*/
static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
/*
* Check arguments; return immediately where possible. For consistency, don't check for null str.
*/
int rightIndex = sourceCount - targetCount;
if (fromIndex < 0) {
return -1;
}
if (fromIndex > rightIndex) {
fromIndex = rightIndex;
}
/* Empty string always matches. */
if (targetCount == 0) {
return fromIndex;
}
// 计算搜索数组的index
int strLastIndex = targetOffset + targetCount - 1;
// 获取搜索数组的最后字符
char strLastChar = target[strLastIndex];
// 满足条件的最小索引
int min = sourceOffset + targetCount - 1;
// 从min后往前加上差值得到开始搜索的索引位置
int i = min + fromIndex;
// 定义一个标签位置
startSearchForLastChar:
while (true) {
// 从后往前搜索最后一个字符的位置
while (i >= min && source[i] != strLastChar) {
i--;
}
// 如果没有找都匹配的字符,直接返回-1
if (i < min) {
return -1;
}
// 找到了匹配的字符,则从 i 以前的位置匹配目标字串
int j = i - 1;
int start = j - (targetCount - 1);
int k = strLastIndex - 1;
while (j > start) {
if (source[j--] != target[k--]) {
i--;
// 如果都不匹配,则将i位置向后移动一位,继续搜索
continue startSearchForLastChar;
}
}
return start - sourceOffset + 1;
}
}
/**
* 返回一个子字符串,从目标索引位置beginIndex到结尾
*
* <blockquote><pre>
* "unhappy".substring(2) returns "happy"
* "Harbison".substring(3) returns "bison"
* "emptiness".substring(9) returns "" (an empty string)
* </pre></blockquote>
*
* @param beginIndex the beginning index, inclusive.
* @return the specified substring.
* @exception IndexOutOfBoundsException if
* {@code beginIndex} is negative or larger than the
* length of this {@code String} object.
*/
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,遵循左闭右开
* Examples:
* <blockquote><pre>
* "hamburger".substring(4, 8) returns "urge"
* "smiles".substring(1, 5) returns "mile"
* </pre></blockquote>
*
* @param beginIndex the beginning index, inclusive.开始索引,包括在内
* @param endIndex the ending index, exclusive.结束索引,不包括在内
* @return the specified substring.子字符串
* @exception IndexOutOfBoundsException if the
* {@code beginIndex} is negative, or
* {@code endIndex} is larger than the length of
* this {@code String} object, or
* {@code beginIndex} is larger than
* {@code 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);
}
/**
* This method is defined so that the {@code String} class can implement
* the {@link CharSequence} interface.
* 实现CharSequence接口,提供一种方式来获取字符串的子序列
*
* @param beginIndex the begin index, inclusive.开始索引,包括在内
* @param endIndex the end index, exclusive.结束索引,不包括在内
* @return the specified subsequence.子字符串
*
* @throws IndexOutOfBoundsException
* if {@code beginIndex} or {@code endIndex} is negative,
* if {@code endIndex} is greater than {@code length()},
* or if {@code beginIndex} is greater than {@code endIndex}
*
* @since 1.4
* @spec JSR-51
*/
public CharSequence subSequence(int beginIndex, int endIndex) {
return this.substring(beginIndex, endIndex);
}
/**
* 将一个字符串连接到当前字符串的结尾
* Examples:
* <blockquote><pre>
* "cares".concat("s") returns "caress"
* "to".concat("get").concat("her") returns "together"
* </pre></blockquote>
*
* @param str the {@code String} that is concatenated to the end
* of this {@code String}.
* @return a string that represents the concatenation of this object's
* characters followed by the string argument's characters.
*/
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没有出现在字符串中,那么直接返回原始字符串
* <p>
* Examples:
* <blockquote><pre>
* "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" (no change)
* </pre></blockquote>
*
* @param oldChar the old character.
* @param newChar the new character.
* @return a string derived from this string by replacing every
* occurrence of {@code oldChar} with {@code newChar}.
*/
public String replace(char oldChar, char newChar) {
// 判断是是否有替换必要
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
// 查找第一个匹配的字符
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
// 检查是否找到了匹配的字符
if (i < len) {
// 将新字符数组初始化为和原始字符串相同的长度
char buf[] = new char[len];
// 将新字符数组初始化为与原始字符串相同的内容
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
/*
使用一个循环来遍历原始字符串中的字符,并根据是否需要替换来填充新字符数组。
如果遇到 oldChar,则插入 newChar;否则,插入原始字符
*/
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this;
}
/**
* 判断字符串是否与给定的正则表达式匹配
*
* @param regex
* the regular expression to which this string is to be matched
*
* @return {@code true} if, and only if, this string matches the
* given regular expression
*
* @throws PatternSyntaxException
* if the regular expression's syntax is invalid
*
* @see java.util.regex.Pattern
*
* @since 1.4
* @spec JSR-51
*/
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
/**
* Returns true if and only if this string contains the specified
* sequence of char values.
* 判断字符串是否包含目标字符串
*
* @param s the sequence to search for
* @return true if this string contains {@code s}, false otherwise
* @since 1.5
*/
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}
/**
* 替换字符串中的第一个匹配给定正则表达式的子串
*
* @param regex
* the regular expression to which this string is to be matched
* @param replacement
* the string to be substituted for the first match
*
* @return The resulting {@code String}
*
* @throws PatternSyntaxException
* if the regular expression's syntax is invalid
*
* @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);
}
/**
* 替换字符串中所有与给定正则表达式匹配的子串
*
* @param regex
* the regular expression to which this string is to be matched
* @param replacement
* the string to be substituted for each match
*
* @return The resulting {@code String}
*
* @throws PatternSyntaxException
* if the regular expression's syntax is invalid
*
* @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);
}
/**
* 替换字符串中所有与给定目标序列匹配的子串
*
* @param target The sequence of char values to be replaced
* @param replacement The replacement sequence of char values
* @return The resulting string
* @since 1.5
*/
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
/**
* 将字符串按照指定的正则表达式进行分割
*
* <p> The string {@code "boo:and:foo"}, 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>{@code { "boo", "and:foo" }}</td></tr>
* <tr><td align=center>:</td>
* <td align=center>5</td>
* <td>{@code { "boo", "and", "foo" }}</td></tr>
* <tr><td align=center>:</td>
* <td align=center>-2</td>
* <td>{@code { "boo", "and", "foo" }}</td></tr>
* <tr><td align=center>o</td>
* <td align=center>5</td>
* <td>{@code { "b", "", ":and:f", "", "" }}</td></tr>
* <tr><td align=center>o</td>
* <td align=center>-2</td>
* <td>{@code { "b", "", ":and:f", "", "" }}</td></tr>
* <tr><td align=center>o</td>
* <td align=center>0</td>
* <td>{@code { "b", "", ":and:f" }}</td></tr>
* </table></blockquote>
*
* <p> An invocation of this method of the form
* <i>str.</i>{@code split(}<i>regex</i>{@code ,} <i>n</i>{@code )}
* yields the same result as the expression
*
* <blockquote>
* <code>
* {@link java.util.regex.Pattern}.{@link
* java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link
* java.util.regex.Pattern#split(java.lang.CharSequence,int) split}(<i>str</i>, <i>n</i>)
* </code>
* </blockquote>
*
*
* @param regex
* the delimiting regular expression
*
* @param limit
* the result threshold, as described above
*
* @return the array of strings computed by splitting this string
* around matches of the given regular expression
*
* @throws PatternSyntaxException
* if the regular expression's syntax is invalid
*
* @see java.util.regex.Pattern
*
* @since 1.4
* @spec JSR-51
*/
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
快捷路径
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
regex只包含一个字符串,而这个字符不是正则表达式的元字符,例如:".$|()[{^?*+\\"
(2)tcwo-har String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
regex是一个双字符的字符串,其中第一个字符是反斜杠\,而第二个字符既不是ASCII数字也不是ASCII字母
*/
char ch = 0;
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))
{
// fastpath
// 当前索引位置
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.add(substring(off, next));
off = next + 1;
} else { // last one
//assert (list.size() == limit - 1);
// 达到限制数量,处理最后一个匹配部分
// 将剩余部分添加到列表中
list.add(substring(off, value.length));
off = value.length;
break;
}
}
// If no match was found, return this
// 如果未找到匹配,返回原字符串数组(即长度为1的数组)
if (off == 0)
return new String[]{this};
// Add remaining segment
// 添加剩余部分到列表中(如果存在)
if (!limited || list.size() < limit)
list.add(substring(off, value.length));
// Construct result
// 构造最终的字符串数组并返回
int resultSize = list.size();
// 如果限制数量为0
if (limit == 0) {
// 循环移除最后一个元素长度为0,则减少resultSize的值
while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
resultSize--;
}
}
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);
}
// 使用正则匹配
return Pattern.compile(regex).split(this, limit);
}
/**
* 将字符串按照指定的正则表达式进行分割
*
* <p> The string {@code "boo:and:foo"}, for example, yields the following
* results with these expressions:
*
* <blockquote><table cellpadding=1 cellspacing=0 summary="Split examples showing regex and result">
* <tr>
* <th>Regex</th>
* <th>Result</th>
* </tr>
* <tr><td align=center>:</td>
* <td>{@code { "boo", "and", "foo" }}</td></tr>
* <tr><td align=center>o</td>
* <td>{@code { "b", "", ":and:f" }}</td></tr>
* </table></blockquote>
*
*
* @param regex
* the delimiting regular expression
*
* @return the array of strings computed by splitting this string
* around matches of the given regular expression
*
* @throws PatternSyntaxException
* if the regular expression's syntax is invalid
*
* @see java.util.regex.Pattern
*
* @since 1.4
* @spec JSR-51
*/
public String[] split(String regex) {
return split(regex, 0);
}
/**
* 用于将多个CharSequence(字符序列)对象以指定的分隔符连接成一个新的字符串
*
* <blockquote>For example,
* <pre>{@code
* String message = String.join("-", "Java", "is", "cool");
* // message returned is: "Java-is-cool"
* }</pre></blockquote>
*
* Note that if an element is null, then {@code "null"} is added.
*
* @param delimiter the delimiter that separates each element,分割每个元素的分隔符
* @param elements the elements to join together.要连接在一起的元素
*
* @return a new {@code String} that is composed of the {@code elements}
* separated by the {@code delimiter}
*
* @throws NullPointerException If {@code delimiter} or {@code elements}
* is {@code null}
*
* @see java.util.StringJoiner
* @since 1.8
*/
public static String join(CharSequence delimiter, CharSequence... elements) {
// 检查非空
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
// Number of elements not likely worth Arrays.stream overhead.
// 使用StringJoiner
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs: elements) {
joiner.add(cs);
}
return joiner.toString();
}
/**
* 用于将多个CharSequence(字符序列)对象以指定的分隔符连接成一个新的字符串
*
* <blockquote>For example,
* <pre>{@code
* List<String> strings = new LinkedList<>();
* strings.add("Java");strings.add("is");
* strings.add("cool");
* String message = String.join(" ", strings);
* //message returned is: "Java is cool"
*
* Set<String> strings = new LinkedHashSet<>();
* strings.add("Java"); strings.add("is");
* strings.add("very"); strings.add("cool");
* String message = String.join("-", strings);
* //message returned is: "Java-is-very-cool"
* }</pre></blockquote>
*
* Note that if an individual element is {@code null}, then {@code "null"} is added.
*
* @param delimiter a sequence of characters that is used to separate each
* of the {@code elements} in the resulting {@code String}
* @param elements an {@code Iterable} that will have its {@code elements}
* joined together.
*
* @return a new {@code String} that is composed from the {@code elements}
* argument
*
* @throws NullPointerException If {@code delimiter} or {@code elements}
* is {@code null}
*
* @see #join(CharSequence,CharSequence...)
* @see java.util.StringJoiner
* @since 1.8
*/
public static String join(CharSequence delimiter,
Iterable<? extends CharSequence> elements) {
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs: elements) {
joiner.add(cs);
}
return joiner.toString();
}
/**
* Converts all of the characters in this {@code String} to lower case using the rules of the given {@code Locale}.
* 使用locale规则将字符串内容变成小写
*
* Case mapping is based on the Unicode Standard version specified by the {@link java.lang.Character Character} class.
* 大小写映射基于{@link java.lang.Character Character}类指定的Unicode标准版本
*
* Since case mappings are not always 1:1 char mappings, the resulting {@code String} may be a different length than the original {@code String}.
* 由于大小写的映射不总是1:1的字符映射,因此生成的String可能与原始的String长度不同
* <p>
* 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 use the case transformation rules for this locale
* @return the {@code String}, converted to lowercase.
* @see java.lang.String#toLowerCase()
* @see java.lang.String#toUpperCase()
* @see java.lang.String#toUpperCase(Locale)
* @since 1.1
*/
public String toLowerCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstUpper;
final int len = value.length;
/* Now check if there are any characters that need to be changed. */
// 检查字符串中是否有任何需要改变的字符
scan: {
for (firstUpper = 0 ; firstUpper < len; ) {
// 获取当前的字符
char c = value[firstUpper];
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
int supplChar = codePointAt(firstUpper);
if (supplChar != Character.toLowerCase(supplChar)) {
break scan;
}
firstUpper += Character.charCount(supplChar);
} else {
// 检查当前字符是否不等于其小写形式
if (c != Character.toLowerCase(c)) {
// 不等于,直接返回
break scan;
}
// 如果当前字符等于其小写形式,那么它只增加firstUpper的值以继续检查下一个字符
firstUpper++;
}
}
// 不存在这返回自身
return this;
}
// 结果数组
char[] result = new char[len];
// 偏移量
int resultOffset = 0; /* result may grow, so i+resultOffset
* is the write location in result */
/* Just copy the first few lowerCase characters. */
// 复制前面的小写字符
System.arraycopy(value, 0, result, 0, firstUpper);
String lang = locale.getLanguage();
// tr 代表土耳其语,az 代表阿塞拜疆语,lt 代表立陶宛语
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] lowerCharArray;
int lowerChar;
int srcChar;
int srcCount;
for (int i = firstUpper; 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 ||
srcChar == '\u03A3' || // GREEK CAPITAL LETTER SIGMA
srcChar == '\u0130') { // LATIN CAPITAL LETTER I WITH DOT ABOVE
// tr 代表土耳其语,az 代表阿塞拜疆语,lt 代表立陶宛语
// 当前字符是希腊大写字母sigma (\u03A3) 或带点的大写字母I (\u0130)
lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
} else {
lowerChar = Character.toLowerCase(srcChar);
}
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);
}
/* Grow result if needed */
// 大小写的映射不总是1:1的字符映射,可能存在扩展的情况
int mapLen = lowerCharArray.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] = lowerCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
// 如果转换后的字符不是增补字符,那么它会被直接复制到结果数组中
result[i + resultOffset] = (char)lowerChar;
}
}
return new String(result, 0, len + resultOffset);
}
/**
* 使用默认的区域设置(locale)将字符串中的所有字符转换为小写,不推荐使用
* @return the {@code String}, converted to lowercase.
* @see java.lang.String#toLowerCase(Locale)
*/
public String toLowerCase() {
return toLowerCase(Locale.getDefault());
}
/**
* Converts all of the characters in this {@code String} to upper case using the rules of the given {@code Locale}.
* 使用locale规则将字符串内容变成大写
*
* Case mapping is based on the Unicode Standard version specified by the {@link java.lang.Character Character}
* class. Since case mappings are not always 1:1 char mappings, the resulting
* {@code String} may be a different length than the original {@code String}.
* <p>
* Examples of locale-sensitive and 1:M case mappings are in the following table.
*
* <table border="1" summary="Examples of locale-sensitive and 1:M case mappings. Shows Language code of locale, lower case, upper case, and description.">
* <tr>
* <th>Language Code of Locale</th>
* <th>Lower Case</th>
* <th>Upper Case</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>tr (Turkish)</td>
* <td>\u0069</td>
* <td>\u0130</td>
* <td>small letter i -> capital letter I with dot above</td>
* </tr>
* <tr>
* <td>tr (Turkish)</td>
* <td>\u0131</td>
* <td>\u0049</td>
* <td>small letter dotless i -> capital letter I</td>
* </tr>
* <tr>
* <td>(all)</td>
* <td>\u00df</td>
* <td>\u0053 \u0053</td>
* <td>small letter sharp s -> two letters: SS</td>
* </tr>
* <tr>
* <td>(all)</td>
* <td>Fahrvergnügen</td>
* <td>FAHRVERGNÜGEN</td>
* <td></td>
* </tr>
* </table>
* @param locale use the case transformation rules for this locale
* @return the {@code String}, converted to uppercase.
* @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;
/* Now check if there are any characters that need to be changed. */
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;
}
/* result may grow, so i+resultOffset is the write location in result */
int resultOffset = 0;
char[] result = new char[len]; /* may grow */
/* 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);
}
/**
* 使用默认的区域设置(locale)将字符串中的所有字符转换为大写,不推荐使用
* @return the {@code String}, converted to uppercase.
* @see java.lang.String#toUpperCase(Locale)
*/
public String toUpperCase() {
return toUpperCase(Locale.getDefault());
}
/**
* 去除开头和结尾的空白字符
*
* @return A string whose value is this string, with any leading and trailing white
* space removed, or this string if it has no leading or
* trailing white space.
*/
public String trim() {
int len = value.length;
int st = 0;
char[] val = value; /* avoid getfield opcode */
while ((st < len) && (val[st] <= ' ')) {
st++;
}
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}
/**
* This object (which is already a string!) is itself returned.
*
* @return the string itself.
*/
public String toString() {
return this;
}
/**
* 将此字符串转换为新的字符数组。
*
* @return a newly allocated character array whose length is the length
* of this string and whose contents are initialized to contain
* the character sequence represented by this string.
*/
public char[] toCharArray() {
// Cannot use Arrays.copyOf because of class initialization order issues
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}
/**
* Returns a formatted string using the specified format string and
* arguments.
*
* <p> The locale always used is the one returned by {@link
* java.util.Locale#getDefault() Locale.getDefault()}.
*
* @param format
* A <a href="../util/Formatter.html#syntax">format string</a>
*
* @param args
* Arguments referenced by the format specifiers in the format
* string. If there are more arguments than format specifiers, the
* extra arguments are ignored. The number of arguments is
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java™ Virtual Machine Specification</cite>.
* The behaviour on a
* {@code null} argument depends on the <a
* href="../util/Formatter.html#syntax">conversion</a>.
*
* @throws java.util.IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the <a
* href="../util/Formatter.html#detail">Details</a> section of the
* formatter class specification.
*
* @return A formatted string
*
* @see java.util.Formatter
* @since 1.5
*/
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
/**
* Returns a formatted string using the specified locale, format string,
* and arguments.
*
* @param l
* The {@linkplain java.util.Locale locale} to apply during
* formatting. If {@code l} is {@code null} then no localization
* is applied.
*
* @param format
* A <a href="../util/Formatter.html#syntax">format string</a>
*
* @param args
* Arguments referenced by the format specifiers in the format
* string. If there are more arguments than format specifiers, the
* extra arguments are ignored. The number of arguments is
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java™ Virtual Machine Specification</cite>.
* The behaviour on a
* {@code null} argument depends on the
* <a href="../util/Formatter.html#syntax">conversion</a>.
*
* @throws java.util.IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the <a
* href="../util/Formatter.html#detail">Details</a> section of the
* formatter class specification
*
* @return A formatted string
*
* @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();
}
/**
* 一系列valueOf,返回{@code Object}参数的字符串表示形式。
*
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
public static String valueOf(char data[]) {
return new String(data);
}
public static String valueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}
public static String copyValueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}
public static String copyValueOf(char data[]) {
return new String(data);
}
public static String valueOf(boolean b) {
return b ? "true" : "false";
}
public static String valueOf(char c) {
char data[] = {c};
return new String(data, true);
}
public static String valueOf(int i) {
return Integer.toString(i);
}
public static String valueOf(long l) {
return Long.toString(l);
}
public static String valueOf(float f) {
return Float.toString(f);
}
public static String valueOf(double d) {
return Double.toString(d);
}
/**
* intern() 方法用于将字符串添加到字符串池中,并返回字符串的引用。如果字符串已经存在于池中,则返回对该字符串的引用;否则,将新字符串添加到池中并返回它的引用。这可以用于减少字符串对象的创建,从而提高性能
* @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/
public native String intern();
}
【源码 JDK 1.8】String的源码解读
最新推荐文章于 2024-09-20 15:32:13 发布