String類
目錄
String類
String的靜態方法
實例方法
字符串類 java中字符串 不是以 '\0'結尾的
String 類實現了三個接口 (須記住實現了CharSequence)
-
字符常量 -- String類型的對象
被共享的地方 稱爲 字符常量池
System.out.println("123456".length() );//6
String constantStr1 ="constant"; // constantStr1引用 指向字符常量線程池
String constantStr2 ="constant"; // constantStr2引用 指向字符常量線程池
System.out.println( constantStr1 == constantStr2 ); //true
// 它們指向的是字符常量線程池中同一內容 故內存地址相同
char [] ch = {'n','e','w'};
String newStr1 = new String (ch);
String newStr2 = new String (ch);
System.out.println( newStr1 == newStr2 ); //false
//new關鍵字 創建對象(申請的不同內存地址)
-
空字符串與null
-
String str1 = new String() ; String str2 = null ; //引用爲null String str3 = "" ;
可知僅有 str1和str3的值相等(String的默認值) 內存地址均不同
String類的聲明 及字段
public final class String implements java.io.Serializable, Comparable<String>, CharSequence{ //字段 private final char value[]; //實際通過封裝字符數組 不可變性 private int hash; //默認0 設長度爲n hash = v[0]*31^(n-1) + v[1]*31^(n-2)+...+v[n-1] /* 31的原因是 可以產生更分散的散列 計算效率高 31*h = (h<<5)-h 用高效移位和減法代替乘法 * hash 的值: * 截取hashCode() * int h = hash; * for (int i = 0; i < value.length; i++) { * h = 31 * h + val[i]; * } */ private static final long serialVersionUID = -6849794470754667710L; private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0]; .... }
構造函數
(除已過時的兩個 還剩13個構造函數 6個byte[] 2個char[] 1個int[](代碼點數組) 1個創建爲空 )
String()//初始化新創建的String對象,使其表示空字符序列。 String(String)//新創建的字符串是參數字符串的副本。 String(StringBuilder) String(StringBuffer) 2個char [] String(char[]){//以char[]初始化新創建的String對象 this.value = Arrays.copyOf(value, value.length); } String(char value[], int offset, int count) //從 offset開始長度爲length 截取數組構造string對象 6個 byte[]相關的構造函數 String(byte[])//String通過使用平臺的默認字符集解碼指定的字節數組來構造new 。 String(byte bytes[], int offset, int length) //從 offset開始長度爲length 截取數組構造string對象 //String通過使用指定的字符集解碼指定的字節數組構造一個new 。 String(byte[], String) String(byte[], Charset) String(byte[] bytes, int offset, int length, String charsetName) String(byte[] bytes, int offset, int length, Charset charset) //int類型的codepoint數組轉化爲String String(int[] codePoints, int offset, int count)
String的靜態方法
public static String
copyValueOf(char data[], int offset, int count)
valueOf(char[],int,int)
copyValueOf(char[] data)
valueOf(char[] )
join(CharSequence delimiter,CharSequence... elements)
返回一個新的String,它由
CharSequenceelements
連接的副本和指定的副本組成delimiter
注意:char不是CharSequence,delimiter必須是雙引號
join
(CharSequence
delimiter, Iterable<? extendsCharSequence
> elements)靜態: 將參數轉換成String類型 valueOf
valueOf
(boolean b)return b ? "true" : "false"; valueOf
(char c)return new String(data, true); 可見性default的 構造函數,第二個參數用來區分public String(char[] c) String
valueOf
(xxx i)//xxx: int long float double
return Xxx.toString(i); format(Locale l, String format, Object... args) 使用指定的語言環境,格式字符串和參數返回格式化的字符串 format(String format, Object... args) 使用指定的格式字符串和參數返回格式化字符串
private boolean nonSyncContentEquals(AbstractStringBuilder sb) 僅被contentEquals(CharSequence cs)調用
針對對StringBuffer類型的內容比較進行加鎖
private int indexOf
Supplementary(int ch, int fromIndex)僅被
indexOf
(int , int )調用處理(罕見的)帶有補充字符的indexOf調用
private int last IndexOf
Supplementary(int ch, int fromIndex)僅被last
IndexOf
(int , int )調用處理(罕見的)帶有補充字符的indexOf調用
實例方法
1.與其他類比較內容是否相等
boolean
contentEquals(CharSequencecs)
將此字符串與指定字符串進行比較
CharSequence
。與
equals
不同 (僅能比較同爲String類型)conten
Equals
包含equals(在String類型被其調用)可判斷自定義實現其CharSequence接口的類的比較
contentEquals(StringBuffer sb)
將此字符串與指定字符串進行比較
StringBuffer
。調用
實現其功能
boolean
equals(Object anObject)
將此字符串與指定的對象進行比較。
覆蓋Object類 boolean
equalsIgnoreCase(String anotherString)
將此
String
與另一個進行比較String
,忽略大小寫return (this == anotherString) ? true : (anotherString != null) && (anotherString.value.length == value.length)
&®ionMatches
(true, 0, anotherString, 0, value.length);boolean
regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
測試兩個字符串區域是否相等。ignoreCase爲true忽略大小寫
boolean
regionMatches(int toffset, String other, int ooffset, int len)
測試兩個字符串區域是否相等。
|
|
||
boolean |
contains(CharSequence s)
當且僅當此字符串包含指定的char值序列時,才返回true。 |
int |
compareTo(String anotherString)
按字典順序比較兩個字符串。 |
boolean |
startsWith(String prefix)
測試此字符串是否以指定的前綴開頭。 |
int |
compareToIgnoreCase(String str)
按字典順序比較兩個字符串,忽略大小寫差異。 |
boolean |
startsWith(String prefix, int toffset)
測試從指定索引開始的此字符串的子字符串是否以指定的前綴開頭。 |
|
|
boolean |
endsWith(String suffix)
測試此字符串是否以指定的後綴結尾。 |
CharSequence |
subSequence(int beginIndex, int endIndex)
返回作爲此序列的子序列的字符序列。 return this.substring(beginIndex, endIndex); |
boolean |
matches(String regex)
判斷此字符串是否與給定的正則表達式匹配。 |
String |
substring(int beginIndex) |
substring(int beginIndex, int endIndex) |
|||
boolean |
isEmpty()
|
subString() 的實現 調用構造函數 String (char value[],int offset,int count) |
|
||
char |
charAt(int index)
返回 |
|
1 | indexOf(int ch)
(從字符數組中的下標爲0的位置開始)。 ch 是作爲字符型對應的整型編碼, |
返回指定子字符串第一次出現的字符串中的索引。 |
2 |
(從字符數組中的下標爲 |
indexOf(String str, int fromIndex)
從指定的索引處開始,返回指定子字符串第一次出現的字符串中的索引。 |
1.1 | lastIndexOf(int ch)
返回指定字符最後一次出現的字符串中的索引。 |
返回指定子字符串最後一次出現的字符串中的索引。 |
2.1 | lastIndexOf(int ch, int fromIndex)
返回指定字符最後一次出現的字符串中的索引,從指定的索引開始向後搜索。 |
lastIndexOf(String str, int fromIndex)
返回指定子字符串最後一次出現的字符串中的索引,從指定索引開始向後搜索。 |
|
|
String |
replace (char oldChar, char newChar)
返回通過替換 |
String |
replace (CharSequence target,CharSequence replacement)
將此字符串中與文字目標序列匹配的每個子字符串替換爲指定的文字替換序列。 |
String |
replaceAll (String regex, String replacement)
將給定替換的給定正則表達式匹配的此字符串的每個子字符串替換。 |
String |
replaceF irst(String regex, String replacement)
將給定替換的給定正則表達式匹配的此字符串的第一個子字符串替換。 |
7.String 與其他類型的轉換方法 |
|
char[]->Sting | String(char [] )構造函數 |
Sting ->char[] |
返回 char[] 方法: toCharArray() |
toCharArray() | 將此字符串轉換爲新的字符數組。 |
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) |
將此字符串中[srcBegin , srcEnd-1]的字符 複製到目標字符數組[dstBegin , ..]中 注意char [] 長度是固定, 若在複製過程中超出字符數組的長度將引起 java.lang.ArrayIndexOutOfBoundsException |
void getChars(char dst[], int dstBegin) //可見性 缺省 |
方法內容:System.arraycopy(value, 0, dst, dstBegin, value.length); 將當期字符數組value(即當前字符串) 的 [0,value.length-1] 複製到 dst 的[dstBegin,dstBegin+value.length-1] |
Sting -> byte [] | 返回 byte[] 方法: getBytes() |
getBytes(String charsetName) |
|
|
應用: "abc" -> 97 98 99 |
getBytes(Charset charset) |
|
8. String 之間 特殊轉換 |
|
將String轉換成 大/小的形式 | |
String toLowerCase() |
String 使用默認語言環境的規則將此中的所有字符轉換爲小寫。 |
String toLowerCase(Locale locale) |
String 使用給定的規則將此中的所有字符轉換爲小寫Locale 。 |
String toUpperCase() |
=String 使用默認語言環境的規則將此中的所有字符轉換爲大寫。 |
String toUpperCase(Locale locale) |
String 使用給定的規則將此中的所有字符轉換爲大寫Locale 。 |
String trim() |
返回一個字符串,其值爲此字符串,並刪除任何前導和尾隨空格。 |
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 將此字符串中的字符複製到目標字符數組中。 | |
String concat(String str) |
將指定的字符串連接到此字符串的末尾 |
將此字符串拆分爲給定正則表達式的匹配項。 | |
String[] |
split(String regex) |
String[] |
split(String regex, int limit) |
9.根據索引將目標字符轉換成codePoint |
|
int offsetByCodePoints(int index, int codePointOffset)
返回此範圍內的索引,該索引 |
|
public native String intern(); | 返回字符串對象的規範表示 |
int codePointAt(int index) |
返回指定索引處的字符(Unicode代碼點)。 |
int codePointBefore(int index) |
返回指定索引之前的字符(Unicode代碼點)。 |
|
返回指定文本範圍內的Unicode代碼點數 |
indexOf源碼
/*
*indexOf(String str)中調用 index(String ,int fromIndex) 給fromIndex傳入0
*String str中str爲目標字符串
*/
public int indexOf(String str, int fromIndex) {
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/*
*該方法爲靜態 可見性爲缺省 作用:針對目標爲字符串String類
*被 AbstractStringBuilder類中indexOf()方法所調用
*/
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);
}
/*
*實現完成 indexOf()方法中與Sting有關的操作
*該方法爲靜態 可見性爲缺省
*下面將被搜索的字符數組 稱爲主串
*char[] source 主串 被調用方的方法填充
*sourceOffset 主串的起始位置 被調用方的方法填充 0
*sourceCount 主串的長度 被調用方的方法填充 count
* #target 目標字串
* targetOffset 目標字串的起始位置 被調用方的方法填充 0
* targetCount 目標串的長度 被調用方的方法填充 target.count
* #fromIndex 開始搜索的位置相對於目標字串的起始位置
*
*返回值 -1 意味着 主串中不含目標字串
* >=0 意味着 主串中含有目標字串
* 返回主串中目標字串出現的下標位置
* 若目標字符爲空 則返回主串的fromIndex 若fromIndex>sourceCount-1 則返回sourceCount
*/
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) { //開始位置大於主串的最大有效位置(sourceCount-1)
return (targetCount == 0 ? sourceCount : -1);//若目標字串不爲空,則返回-1
}
if (fromIndex < 0) { //若fromIndex >= sourceCount 則意味着 有效字串實際上爲空
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount);
//以source[i] == targetOffset 作爲判斷進入循環的條件
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) { //之前的while循環的判斷條件 i先自增再判斷 所以
int j = i + 1;//i已遍歷
int end = j + targetCount - 1;//遍歷完target時的主串下標的位置
//進入循環
// 退出循環:元素不一致或遍歷完目標字串
for (int k = targetOffset + 1; j < end && source[j]
== target[k]; j++, k++);//k標記
//若循環遍歷完目標字串
if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}