上文:jdk-8大基础类型源码阅读(byte、short、int、long、float、double、boolean、char)
背景
编写了大量String类的相关代码,但是String在jdk层面到底支持哪些功能,或者说有哪些用法我们平常很少注意到,那么通过阅读String的源码最好来了解。
源码阅读
源码位置:java.lang.String
代码语言:javascript复制package com.string;
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.Objects;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
*
* 功能描述: String 源码解析
*
* 本源码基于JDK 1.8
*
* @param:
* @return:
* @auther: csh
* @date: 2022/9/4 3:54 下午
*/
public class StringSource
implements java.io.Serializable, Comparable<String>, CharSequence {
/** 定义字符字类型值数组*/
private final char value[];
/**缓存字符串的哈希码 */
private int hash; // Default to 0
/** 序列化id */
private static final long serialVersionUID = -6849794470754667710L;
/**
* Class String is special cased within the Serialization Stream Protocol.
* String类在序列化流协议中是特殊的。貌似没找着使用地方。
*
* A String instance is written into an ObjectOutputStream according to
* <a href="{@docRoot}/../platform/serialization/spec/output.html">
* Object Serialization Specification, Section 6.2, "Stream Elements"</a>
*/
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
/**
* 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的值为""
*/
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.
*
* @param original
* A {@code String}
*
* 通过 new String("xx") 构建方法创建的值,并且会计算该值的哈希码
*/
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.
*
* @param value
* The initial value of the string
* 通过字符数组创建String
*/
public String(char value[]) {
//通过arrays.copyOf进行赋值,传长度是为了方便在new chare(length)创建长度
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.
*
* @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
* String 构建方法创建之一
* char value[] 字符数组 就要是内容
* int offset 偏移量
* int count 数量
*/
public String(char value[], int offset, int count) {
//小于0直接抛出数组角标越界
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
//数量小于或等于0
if (count <= 0) {
//同样抛出数组角标越界
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
//如果偏移量小于等于值长度,重新将当前的内容赋值为空,并返回。
if (offset <= value.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
//向右移一位减去数量
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset count);
}
//复制区间
this.value = Arrays.copyOfRange(value, offset, offset count);
}
/**
*
* @param codePoints 内容
* @param offset 偏移量
* @param count 长度
*/
public String(int[] codePoints, int offset, int count) {
//小于0抛出异常
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
//长度小于等于0
if (count <= 0) {
//小于0跑出异常
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
//如果小于内容长度小于偏移量将值赋为"" 并且返回
if (offset <= codePoints.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
//向右移一位减去数量
if (offset > codePoints.length - count) {
throw new StringIndexOutOfBoundsException(offset count);
}
//偏移量加上数量
final int end = offset count;
// Pass 1: Compute precise size of char[]
int n = count;
//计算char的精确大小[]
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:分配并填充char[]
final char[] v = new char[n];
for (int i = offset, j = 0; i < end; i , j ) {
int c = codePoints[i];
if (Character.isBmpCodePoint(c))
v[j] = (char)c;
else
Character.toSurrogates(c, v, j );
}
this.value = v;
}
/**该方法已删除标识 **/
@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;
}
/**
* 该方法已快删除
*/
@Deprecated
public String(byte ascii[], int hibyte) {
this(ascii, hibyte, 0, ascii.length);
}
/**
* 检查异常
* @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);
}
/**
* 指定字符集赋值
* @param bytes 字节数组(内容)
* @param offset 偏移量
* @param length 长度
* @param charsetName 字符集编码名称默认为:ISO-8859-1
* @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);
}
/**
* 不带字符集赋值(同上类似)
* @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);
}
/**
* 指定字符集赋值 调用以上
* @param bytes
* @param charsetName
* @throws UnsupportedEncodingException
*/
public String(byte bytes[], String charsetName)
throws UnsupportedEncodingException {
this(bytes, 0, bytes.length, charsetName);
}
/**
* 指定字符集赋值创建的构建方法
* @param bytes
* @param charset
*/
public String(byte bytes[], Charset charset) {
this(bytes, 0, bytes.length, charset);
}
/**
* 不带字符集的构建方法
* @param bytes
* @param offset
* @param length
*/
public String(byte bytes[], int offset, int length) {
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(bytes, offset, length);
}
/**
* 字节数组构建方法
* @param bytes 字节列表
*/
public String(byte bytes[]) {
this(bytes, 0, bytes.length);
}
/**
* 通过StringBuffer创建的构建方法 线程安全
* @param buffer
*/
public String(StringBuffer buffer) {
//同步锁
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
}
/**
* 通过StringBuilder 创建的构建方法 非线程安全
* @param builder
*/
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
/**
* 通过 字节数组创建string
* @param value
* @param share
*/
String(char[] value, boolean share) {
// assert share : "unshared not supported";
this.value = value;
}
/**
* 返回当前字符串的长度
* @return
*/
public int length() {
return value.length;
}
/**
* 判断当前的字符串是否为空,以lenth
* @return
*/
public boolean isEmpty() {
return value.length == 0;
}
/**
* 获取字符串的位数的char数据
* @param index 坐标
* @return
*/
public char charAt(int index) {
//小于0或长度大于字符串长度抛出 数组角标越界
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
/**
* 返回指定位置的字符转成的int
* @param index
* @return
*/
public int codePointAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointAtImpl(value, index, value.length);
}
/**
*
* @param index
* @return 获取当前位置前一位的字符的int
*/
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码位数。
* @param beginIndex
* @param endIndex
* @return
*/
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);
}
/**
* 返回此String中的索引,该索引与给定索引的偏移量为codePointOffset代码点。
* @param index
* @param codePointOffset
* @return
*/
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.
* This method doesn't perform any range checking.
* 从dstBegin开始将字符从该字符串复制到dst中。
*/
void getChars(char dst[], int dstBegin) {
System.arraycopy(value, 0, dst, dstBegin, value.length);
}
/**
* 从srcBegin开始将字符从该字符串复制到dst中。
* @param srcBegin
* @param srcEnd
* @param dst
* @param dstBegin
*/
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
//小于0跑出数组角标越界
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
//srcEnd大于数据最长抛出异常
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个高阶位不会被复制,也不会以任何方式参与传输。
* @param srcBegin
* @param srcEnd
* @param dst
* @param dstBegin
*/
@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 ];
}
}
/**
* 获取指定字符集的字节数组
*/
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException {
if (charsetName == null) throw new NullPointerException();
return StringCoding.encode(charsetName, value, 0, value.length);
}
/**
* 使用给定字符集将该String编码为一个字节序列,并将结果存储到一个新的字节数组中。
* @param charset
* @return
*/
public byte[] getBytes(Charset charset) {
if (charset == null) throw new NullPointerException();
return StringCoding.encode(charset, value, 0, value.length);
}
/**
* 将所有数数以字节数组方式返回
* @return
*/
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}
/**
* 对于两个值内容是否一致。
* @param anObject
* @return
*/
public boolean equals(Object anObject) {
//如果连内存地址一致直接返回true
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;
//循环对比每一位,如果都一样返回true,反之存在一位不一样返回false
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i ;
}
return true;
}
}
//默认返回false,所以这里也要注意,仅能对比String类型字符串,这里有个搞,如果你传进来是一个integer也是oK但是判断是直接走false
return false;
}
/**
* 通过内容方法是否一致方法
* @param sb
* @return
*/
public boolean contentEquals(StringBuffer sb) {
return contentEquals((CharSequence)sb);
}
/**
* 私有方法 不是同步锁方式对比内容
* @param sb
* @return
*/
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;
}
/**
* 对比内容是否一致方法 安全方法
* @param cs
* @return
*/
public boolean contentEquals(CharSequence cs) {
// Argument is a StringBuffer, StringBuilder
//判断是否为builder的类型
if (cs instanceof AbstractStringBuilder) {
//判断是否为StringBuffer类型
if (cs instanceof StringBuffer) {
//通过同步锁进行匹配
synchronized(cs) {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
} else {
//不是StringBuffer类型 不加锁判断
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
}
// 字符串类型直接调用equals
if (cs instanceof String) {
return equals(cs);
}
// Argument is a generic CharSequence
char v1[] = value;
int n = v1.length;
//都不是则判断长度是否不一致
if (n != cs.length()) {
return false;
}
//如果长度判断一样,最后通过这个来匹配每一位上面的字符是否一样,如果一样则返回true,否则就是false
for (int i = 0; i < n; i ) {
if (v1[i] != cs.charAt(i)) {
return false;
}
}
return true;
}
/**
* 不区分大小对比内容
* @param anotherString 内容
* @return
*/
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. The result is a
* positive integer if this {@code String} object lexicographically
* follows the argument 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}.
* <p>
* 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 < 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:
* <blockquote><pre>
* this.charAt(k)-anotherString.charAt(k)
* </pre></blockquote>
* If there is no index position at which they differ, then the shorter
* string lexicographically precedes the longer string. In this case,
* {@code compareTo} returns the difference of the lengths of the
* strings -- that is, the value:
* <blockquote><pre>
* this.length()-anotherString.length()
* </pre></blockquote>
*
* @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}. This comparator is serializable.
* <p>
* 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.
*
* @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 {
// 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.
* <p>
* 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.
* <p>
* A substring of this {@code String} object is compared to a substring
* of the argument other. The result is true if these substrings
* represent identical character sequences. The substring of this
* {@code String} object to be compared begins at index {@code toffset}
* and has length {@code len}. The substring of other to be compared
* begins at index {@code ooffset} and has length {@code len}. The
* result is {@code false} if and only if at least one of the following
* is true:
* <ul><li>{@code toffset} is negative.
* <li>{@code ooffset} is negative.
* <li>{@code toffset len} is greater than the length of this
* {@code String} object.
* <li>{@code ooffset len} is greater than the length of the other
* argument.
* <li>There is some nonnegative integer <i>k</i> less than {@code len}
* such that:
* {@code this.charAt(toffset }<i>k</i>{@code ) != other.charAt(ooffset }
* <i>k</i>{@code )}
* </ul>
*
* @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
* 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;
}
/**
* 测试两个字符串区域是否相等。
* @param ignoreCase true转成大写 false小写
* @param toffset
* @param other
* @param ooffset
* @param len
* @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;
// 注意:toffset、ooffset或len可能在-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) {
// If characters don't match but case may be ignored,
// try converting both characters to uppercase.
// If the results match, then the comparison scan should
// continue.
//统一转成大写
char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2);
if (u1 == u2) {
continue;
}
// Unfortunately, conversion to uppercase does not work properly
// for the Georgian alphabet, which has strange rules about case
// conversion. So we need to make one last check before
// exiting.
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}
}
return false;
}
return true;
}
/**
* 测试 指定位置开头是不是以 prefix开头的字符串
* @param prefix
* @param toffset
* @return
*/
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;
}
/**
* 测试是不是以某个字符串开头的 默认从0位开始
* @param prefix
* @return
*/
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
/**
* 测试是不是由指定字符串结束 ,默认为长度-开头长度开始
* @param suffix
* @return
*/
public boolean endsWith(String suffix) {
return startsWith(suffix, value.length - suffix.value.length);
}
/**
* 获取哈希码方法
* @return
*/
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
* @return
*/
public int indexOf(int ch) {
return indexOf(ch, 0);
}
/**
* 从指定开始位置,获取第几位的坐标,如果换不到返回-1
* @param ch
* @param fromIndex
* @return
*/
public int indexOf(int ch, int fromIndex) {
final int max = value.length;
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= max) {
// Note: fromIndex might be near -1>>>1.
return -1;
}
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// handle most cases here (ch is a BMP code point or a
// negative value (invalid code point))
final char[] value = this.value;
for (int i = fromIndex; i < max; i ) {
if (value[i] == ch) {
return i;
}
}
return -1;
} else {
return indexOfSupplementary(ch, fromIndex);
}
}
/**
* 用一个补充字符处理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;
}
/**
* 返回指定字符最后一次出现的字符串中的索引。对于ch在0到0xFFFF(包括)范围内的值,返回的索引(以Unicode代码单位)是最大的值k.
* @param ch
* @return
*/
public int lastIndexOf(int ch) {
return lastIndexOf(ch, value.length - 1);
}
/**
* 返回指定字符最后一次出现的字符串中的索引
* @param ch
* @param fromIndex
* @return
*/
public int lastIndexOf(int ch, int fromIndex) {
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// handle most cases here (ch is a BMP code point or a
// negative value (invalid code point))
final char[] value = this.value;
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);
}
}
/**
* 用一个补充字符处理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
* @return
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
/**
* 指定位置开始寻找字符串位置。
* @param str
* @param fromIndex
* @return
*/
public int indexOf(String str, int fromIndex) {
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/**
* 同上作用。
* @param source 来源数据
* @param sourceOffset 来源偏移量
* @param sourceCount 来源数量
* @param target 目标内容
* @param fromIndex 开始位置
* @return
*/
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);
}
/**
* 同上
* @param source
* @param sourceOffset
* @param sourceCount
* @param target
* @param targetOffset
* @param targetCount
* @param fromIndex
* @return
*/
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
* @return
*/
public int lastIndexOf(String str) {
return lastIndexOf(str, value.length);
}
/**
* 指定开始位置,寻找字符串最后一个位置
* @param str
* @param fromIndex
* @return
*/
public int lastIndexOf(String str, int fromIndex) {
return lastIndexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/**
* 同上
* @param source 来源数据
* @param sourceOffset 来源偏移量
* @param sourceCount 来源数量
* @param target 目标内容
* @param fromIndex 开始位置
* @return
*/
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);
}
/**
* 最终定位的方法(获取字符串)
* @param source
* @param sourceOffset
* @param sourceCount
* @param target
* @param targetOffset
* @param targetCount
* @param fromIndex
* @return
*/
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;
//开始小于0返回-1
if (fromIndex < 0) {
return -1;
}
//开始大于右边位置 将右边位置赋值给开始位置
if (fromIndex > rightIndex) {
fromIndex = rightIndex;
}
/* 如果目标数量为0返恩啊开始位置 */
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--;
}
//小于最小值返回-1
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;
}
}
//最终返回开始位减去偏移量 1
return start - sourceOffset 1;
}
}
/**
* 从指定位置开始裁剪 比如 abc 输入 1只剩下 bc
* @param beginIndex
* @return
*/
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);
}
/**
* 指字开始和结束位置进行裁剪字符串
* @param beginIndex
* @param endIndex
* @return
*/
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);
}
/**
* 指定开始和结束位置进行裁剪字符串,并返回字节数组
* @param beginIndex
* @param endIndex
* @return
*/
public CharSequence subSequence(int beginIndex, int endIndex) {
return this.substring(beginIndex, endIndex);
}
/**
* 拼接字符串到形成新的字符串 比如 abc de = abcde
* @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);
}
/**
* 替换字符 注意:仅替换一位
* @param oldChar 老字符
* @param newChar 新字符
* @return
*/
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
//循环替换 注意这里仅替换一位
while ( i < len) {
if (val[i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0; j < i; j ) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i ;
}
return new String(buf, true);
}
}
return this;
}
/**
* 通过正规表达式方式进行匹配
* @param regex
* @return
*/
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
/**
* 判断是否包含字符
* @param s
* @return
*/
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}
/**
* 通过正则表达式方式替换首位字符串
* @param regex
* @param replacement
* @return
*/
public String replaceFirst(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}
/**
* 通过正式表达式方式替换所有匹配正式的字符串
* @param regex
* @param replacement
* @return
*/
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
/**
* 替换所有字符串并返回替换后的结果
* @param target
* @param replacement
* @return
*/
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
/**
* 通过正式表达式方式进行分割
* @param regex 表达式
* @param limit 长度
* @return
*/
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
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
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))
{
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
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();
if (limit == 0) {
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);
}
/**
* 分割,从0位坐标开始
* @param regex
* @return
*/
public String[] split(String regex) {
return split(regex, 0);
}
/**
* 将字符拼接到字符串后面。
* @param delimiter
* @param elements
* @return
*/
public static String join(CharSequence delimiter, CharSequence... elements) {
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
// Number of elements not likely worth Arrays.stream overhead.
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs: elements) {
joiner.add(cs);
}
return joiner.toString();
}
/**
* 同上类似
* @param delimiter
* @param elements
* @return
*/
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();
}
/**
* 将当前字符串转换成指定语言的小写字符串
* @param locale
* @return
*/
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 ;
}
}
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();
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
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 */
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);
}
/**
* 转换为小写字符串
* @return
*/
public String toLowerCase() {
return toLowerCase(Locale.getDefault());
}
/**
* 将字符串转换为指定语言大写的字符串
* @param locale
* @return
*/
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);
}
/**
* 直接转换为大写的字符串
* @return
*/
public String toUpperCase() {
return toUpperCase(Locale.getDefault());
}
/**
* 去掉前后空格
* @return
*/
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;
}
/** 将内容转成字符串 **/
public String toString() {
return this;
}
/**
* 将内容转成字符数组
* @return
*/
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;
}
/**
* 格式化内容
* @param format 格式
* @param args 参数
* @return
*/
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
/**
* 同上 附加了语言
* @param l
* @param format
* @param args
* @return
*/
public static String format(Locale l, String format, Object... args) {
return new Formatter(l).format(format, args).toString();
}
/**
* 将内值转成string 如果为空转输出null
* @param obj
* @return
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
/**
* 将字符数组转成字符串
* @param data
* @return
*/
public static String valueOf(char data[]) {
return new String(data);
}
/**
* 将字符数组转成字符串
* @param data 内容
* @param offset 偏移量
* @param count 长度
* @return
*/
public static String valueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}
/**
* 复制内容创建字符串
* @param data
* @param offset
* @param count
* @return
*/
public static String copyValueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}
/**
* 通过字符数组进行复制
* @param data
* @return
*/
public static String copyValueOf(char data[]) {
return new String(data);
}
/**
* 将布尔转成字符串 true 和false
* @param b
* @return
*/
public static String valueOf(boolean b) {
return b ? "true" : "false";
}
/**
* 将字符转成字符串
* @param c
* @return
*/
public static String valueOf(char c) {
char data[] = {c};
return new String(data, true);
}
/**
* 将整弄转成字符串
* @param i
* @return
*/
public static String valueOf(int i) {
return Integer.toString(i);
}
/**
* 将Long类型转成字符串
* @param l
* @return
*/
public static String valueOf(long l) {
return Long.toString(l);
}
/**
* 将浮点型转成字符串
* @param f
* @return
*/
public static String valueOf(float f) {
return Float.toString(f);
}
/**
* 将double转成字符串
* @param d
* @return
*/
public static String valueOf(double d) {
return Double.toString(d);
}
/**
* 通过字符串池中获取字符串,该对象也会被添加池中 native为本地方法由c 维护看不了源码
* @return
*/
public native String intern();
}
最后
以上是基于String的源码进行阅读以个人的理解方式进行解析,当然可能存在不足或不准,如有误请谅解。本人仅翻译基于jdk1.8的java层面的内容,当然还有大量C 层面未读取,后续有时间的话定当继续深入学习,如果有这方面的需求同学可以自行先深入了解,以便交流学习。
扩展阅读
https://blog.csdn.net/u010700335/article/details/40979037
https://blog.csdn.net/GoodburghCottage/article/details/126639829
https://www.cnblogs.com/listenfwind/p/8450241.html