文章目錄
- 概述
- 實現接口與父類
- 構造器
- String(String original)
- String(char value[])
- String(char value[], int offset, int count)
- String(StringBuffer buffer)
- String(stringBuilder builder)
- 字段
- 方法
- char charAt(int index)
- boolean equals(Object anObject)
- String substring(int beginIndex)
- boolean regionMatches(int toffset, String other, int ooffset, int len)
- boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
- int indexOf(int ch, int fromIndex)
- boolean startsWith(String prefix, int toffset)
- boolean endsWith(String suffix)
- native String intern()
- public int hashCode()
- String trim()
- public String replace(CharSequence target, CharSequence replacement)
- String[] split(String regex, int limit)
- public char[] toCharArray()
- int compareTo(String anotherString)
- boolean contentEquals(CharSequence cs)
- String concat(String str)
- public String toLowerCase(Locale locale)
- public int compareToIgnoreCase(String str)
- int codePointBefore(int index)
- static String valueOf(char c)
- static String copyValueOf(char data[], int offset, int count)
- static String join(CharSequence delimiter, CharSequence... elements)
概述
-
String類代表字符串
-
位置:java.lang.String
實現接口與父類
Serializable
- 用於實現序列化
Comparable
- 用於比較順序
CharSequence
- 指定字符串的部分方法
構造器
String(String original)
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
String(char value[])
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length); // 按照數組的長度,複製數組到value
}
String(char value[], int offset, int count)
public String(char value[], int offset, int count) {
if (offset < 0) { //判斷偏置是否小於0,小於0拋異常
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) { //判斷截取的字符數
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value; // 直接返回空字符串
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) { // 偏置字符 + 偏移數 超出數組邊界
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count); // 複製截取的部分,範圍複製
}
String(StringBuffer buffer)
- StringBuffer是線程安全的,也是可變的字符序列
public String(StringBuffer buffer) {//線程安全,可變的字符序列.字符串緩衝區就像一個String ,但可以修改
synchronized(buffer) { // 同步機制
this.value = Arrays.copyOf(buffer.getValue(), buffer.length()); // 將buffer中的字符串數據複製到value中
}
}
String(stringBuilder builder)
- StringBuilder和StringBuffer類似,但不保證同步
- 常用於單線程
public String(StringBuilder builder) {//StringBuilder和StringBuffer類似,但不保證同步
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
字段
字段名 | 說明 |
---|---|
value | 存儲字符 |
hash | 存儲hashCode |
serialVersionUID | 序列號 |
CASE_INSENSITIVE_ORDER | 一個比較器 |
方法
char charAt(int index)
- 返回索引位處的字符
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) { //索引值小於零或者大於字符串長度
throw new StringIndexOutOfBoundsException(index); //"String index out of range: " + index
}
return value[index]; //value是字符數組,String中的值是通過數組存儲的
}
boolean equals(Object anObject)
- 判斷兩個字符串是否相等
public boolean equals(Object anObject) {
if (this == anObject) { //比較內存地址(引用類型),而基本數據類型是比較值
return true;
}
if (anObject instanceof String) {//如果anObject是String類型
String anotherString = (String)anObject;//強制轉換
int n = value.length; //this字符串的長度
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;
}
String substring(int beginIndex)
- 返回子字符串
public String substring(int beginIndex) {
if (beginIndex < 0) { //索引小於0
throw new StringIndexOutOfBoundsException(beginIndex); //"String index out of range: " + index
}
int subLen = value.length - beginIndex; //子串的長度
if (subLen < 0) { //索引超過數據長度
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen); //直接根據構造函數new一個子串
}
boolean regionMatches(int toffset, String other, int ooffset, int len)
- 測試兩個字符串區域是否相等(給定開始位的索引)
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;
}
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
- 測試兩個字符串的子區域是否相等,存在忽略大小寫選項
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) { //忽略大小寫,比較方法就是將要比較的字母都轉換成大寫或小寫
// 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;
}
int indexOf(int ch, int fromIndex)
- 返回指定字母在字符串中第一次出現時的索引,從指定的索引開始搜索
public int indexOf(int ch, int fromIndex) { // 返回某個字母在字符串中第一次出現的索引
final int max = value.length; // 不可修改
if (fromIndex < 0) { // 開始索引爲負數則直接置爲0
fromIndex = 0;
} else if (fromIndex >= max) { //索引超過字符串長度,直接返回-1
// 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; //沒有對應字母,返回-1
} else { //是補充字母
return indexOfSupplementary(ch, fromIndex);
}
}
boolean startsWith(String prefix, int toffset)
- 測試在指定索引出開始的此字符串的子字符串是否以指定的前綴開頭
public boolean startsWith(String prefix, int toffset) {
char ta[] = value;
int to = toffset;
char pa[] = prefix.value;
int po = 0;
int pc = prefix.value.length;
// Note: toffset might be near -1>>>1.
if ((toffset < 0) || (toffset > value.length - pc)) {
return false; //索引小於0或者字符串減去前綴的長度
}
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
return true;
}
boolean endsWith(String suffix)
- 判斷是否以指定字符串爲結尾
public boolean endsWith(String suffix) {
return startsWith(suffix, value.length - suffix.value.length);
}
native String intern()
-
判斷這個常量是否存在於常量池。
如果存在
判斷存在內容是引用還是常量,
如果是引用,
返回引用地址指向堆空間對象,
如果是常量,
直接返回常量池常量
如果不存在,
將當前對象引用複製到常量池,並且返回的是當前對象的引用 -
參考博文: https://blog.csdn.net/u013366617/article/details/83618361
public int hashCode()
- 返回String對象的哈希碼
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]; //注意int類型數據的越界問題
}
hash = h;
}
return h;
}
String trim()
- 刪除所有前導空格和尾隨空格
- 查看ASCII碼錶 https://blog.csdn.net/qq_35831134/article/details/90484568
public String trim() { //刪除所有前導空格和尾隨空格
int len = value.length;
int st = 0; //首位
char[] val = value; /* avoid getfield opcode */
while ((st < len) && (val[st] <= ' ')) { //空格的ASCII碼是32,是有效字符中最小的一個
st++;
}
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}
public String replace(CharSequence target, CharSequence replacement)
- 將字符串的指定字符進行替換
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]; // 將之前的字符數組拷貝到buf數組
}
while (i < len) { // 逐個字母開始判斷
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this; // 沒有字符需要替換,直接返回this字符串
}
String[] split(String regex, int limit)
- 通過正則表達式regex對字符串進行分割
- 結果返回字符串數組
- limit爲結果閾值
public String[] split(String regex, int limit) {
char ch = 0;
// if判斷的條件有3中:
// 1、如果 匹配規則regex長度爲1 且 不是 ".$|()[{^?*+\\" 中的特殊字符
// 2、 匹配規則regex長度爲2 且 第一個字符爲轉義字符\,第二個字符不是字母或數字
// 3、 編碼
// 並給 ch 賦值
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))
{
// off和next 分別表示 截取子串時的上下索引,初始都爲0
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) {
// 當 off=next時 截取的是空串
list.add(substring(off, next));
// 子串截完以後 下次截取的初始索引從next的下一位開始
off = next + 1;
} else { // last one
list.add(substring(off, value.length));
off = value.length;
break;
}
}
if (off == 0)
return new String[]{this};
if (!limited || list.size() < limit)
list.add(substring(off, value.length));
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);
}
public char[] toCharArray()
- 將此字符串轉換爲新的字符數組
public char[] toCharArray() {
// Cannot use Arrays.copyOf because of class initialization order issues
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length); //本地方法
return result;
}
int compareTo(String anotherString)
- 按字典順序比較兩個字符串
- 比較是基於字符串中每個字符的Unicode值
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) { // 比較lim內個字符
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) { //第一個字符不相等的Unicode值
return c1 - c2;
}
k++;
}
return len1 - len2; //都相等(兩字符串一致),返回0
}
boolean contentEquals(CharSequence cs)
public boolean contentEquals(CharSequence cs) { //CharSequence有三個接口實現的類,需要分開處理
// Argument is a StringBuffer, StringBuilder
if (cs instanceof AbstractStringBuilder) { //判斷類型
if (cs instanceof StringBuffer) { //StringBuffer
synchronized(cs) { //同步方法,加鎖,StringBuffer是線程安全的,需要處理併發場景
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
} else { //StringBuilder是單線程的
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
}
// Argument is a String
if (cs instanceof String) {
return equals(cs); //String的equals方法
}
// Argument is a generic CharSequence
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;
}
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;
}
String concat(String str)
- 將指定的字符串連接到該字符串的末尾
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) { // 字符串爲空,直接返回
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen); //新建一個字符數組,長度爲兩個字符串長度之和
str.getChars(buf, len); //將str複製到數組中,位置從value.length後開始
return new String(buf, true); // 返回參數是char數組的String 構造函數
}
public String toLowerCase(Locale locale)
- 基於地區Locale 將字符串轉換成小寫
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: { // 這是帶標籤的break語句
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);
}
public int compareToIgnoreCase(String str)
- 按字典順序比較兩個字符串,不考慮大小寫
public int compareToIgnoreCase(String str) {
return CASE_INSENSITIVE_ORDER.compare(this, str);
}
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; }
}
int codePointBefore(int index)
- 返回指定索引前的代碼點
static String valueOf(char c)
- 返回char類型的String表達
public static String valueOf(char c) {
char data[] = {c};//轉成char數組
return new String(data, true);//調用構造函數
}
static String copyValueOf(char data[], int offset, int count)
static String join(CharSequence delimiter, CharSequence… elements)
- 組成一個新的字符串,elements與指定的delimiter一起加入副本
String message = String.join("-", "Java", "is", "cool"); // message returned is: "Java-is-cool"
public static String join(CharSequence delimiter, CharSequence... elements) { // elments用delimiter進行組合,返回一個字符串
Objects.requireNonNull(delimiter); //判斷非空
Objects.requireNonNull(elements);
// Number of elements not likely worth Arrays.stream overhead.
StringJoiner joiner = new StringJoiner(delimiter); //字符串構造器,分隔符由delimiter指定
for (CharSequence cs: elements) {
joiner.add(cs); //逐個添加,分隔符在add方法中已經添加
}
return joiner.toString(); // 轉換爲String對象
}