一、java.lang.String
1、string對象不可變,被final修飾,不能被繼承。
2、賦值方式:
(1)直接賦值。比如: String name = "rick";
(2)使用new關鍵字創建。比如:String name = new String("rick");
3、常用構造方法:
(1)public String(); //無參構造方法,用來創建空字符串的String對象。
(2)public String(String original); //用已知的字符串original創建一個String對象。
(3)public String(char value[]); //用字符數組value創建一個String對象。
(4)public String(char value[], int offset, int count); //用字符數組value的offset開始的count個字符創建一個String對象。
(5)public String(byte bytes[]); //用字節數組bytes創建一個String對象。
(6)public String(byte bytes[], String charsetName); //根據指定編碼格式charsetName,用字節數組bytes創建一個String對象。
(7)public String(byte bytes[], int offset, int length, String charsetName); //根據指定編碼格式charsetName,用字節數組bytes的offset開始的length個字符創建一個String對象。
4、常用方法:
(1)public int length() ; //返回該字符串的長度。
(2)public boolean isEmpty() ; //判斷字符串是否為空。
(3)public char charAt(int index) ; //返回字符串中指定位置的字符;字符串范圍為 0 ~ length()-1。
(4)public byte[] getBytes(String charsetName); //根據指定編碼格式charsetName,將字符串轉為字節數組。
(5)public boolean equals(Object anObject); //用於比較兩個字符串內容是否相同。相同返回true,不同返回false。
(6)public boolean equalsIgnoreCase(String anotherString); //與equals比較類似,但忽略大小寫。
(7)public int compareTo(String anotherString); //按字典順序逐個字符進行比較,返回的數即為字符間的差距(大小關系)。若字符大於anotherString的字符,則返回正數,小於則返回負數,等於則返回0。
(8)public int compareToIgnoreCase(String str); //與compareTo類似,但忽略大小寫。
(9)public boolean startsWith(String prefix); //判斷字符串是否以指定字符串prefix開頭。
(10)public boolean endsWith(String suffix); //判斷字符串是否以指定字符串suffix結尾。
(11)public int hashCode(); //用於返回字符串的哈希碼。
(12)public boolean contains(CharSequence s); //判斷當前字符串是否包含 給定的子字符串s。
(13)public String concat(String str) ; //用於字符串的拼接。等價於 符號 “+”。
(14)public String trim(); //用於去除字符串首尾兩端的空格。
(15)public String toLowerCase(); //將字符串全轉成小寫字符串。
(16)public String toUpperCase(); //將字符串全轉為大寫字符串。
(17)public char[] toCharArray() ; //將字符串轉為字符數組。
(18)public String substring(int beginIndex); //從當前字符串中的beginIndex位置起,取出剩余的字符作為一個新的字符串返回。
(19)public String substring(int beginIndex, int endIndex); //從當前字符串中的beginIndex位置起,取出到endIndex-1位置的字符作為一個新的字符串返回。
(20)public String replace(char oldChar, char newChar); //用字符newChar替換當前字符串中所有的oldChar字符,並返回一個新的字符串。
(21)public String replaceAll(String regex, String replacement);將字符串中符合regex格式的子串替換成replacement,此時並未改變原始字符串。
(22)public String[] split(String regex); //將字符串使用regex標記分割,並將分割后的單詞存入字符串數組中。
(23)public boolean matches(String regex); //判斷當前字符串對象是否與參數regex格式相匹配。
二、java.lang.StringBuffer、java.lang.StringBuilder
1、當頻繁修改字符串時,需要使用 StringBuffer 和 StringBuilder 類。與String 類不同的是,StringBuffer 和 StringBuilder 類的對象能夠被多次的修改,並且不產生新的未使用對象。
2、StringBuffer與StringBuilder大體上相似,只是StringBuffer屬於線程安全的。對於單線程的程序,推薦使用StringBuilder。
3、常用構造方法:(以StringBuffer為例)
(1)public StringBuffer(); //默認構造一個不帶字符的字符串緩沖區,初始容量為16個字符。
(2)public StringBuffer(String str); //構造一個str.length() + 16的字符串緩沖區。
4、常用方法:(以StringBuffer為例)
(1)追加:
public synchronized StringBuffer append(Object obj); //將對象追加到字符串的末尾。
(2)插入:
public StringBuffer insert(int dstOffset, CharSequence s) ; //將字符串s插入到指定位置 dstOffset。字符串范圍為 0 ~ length()-1。
(3)刪除:
public synchronized StringBuffer delete(int start, int end); //刪除start ~ end-1 范圍的字符。
public synchronized StringBuffer deleteCharAt(int index); //刪除指定位置的字符。
(4)替換:
public synchronized StringBuffer replace(int start, int end, String str); //用給定 String
中的字符替換此序列(start ~ end-1)的子字符串中的字符。
public synchronized void setCharAt(int index, char ch); //用給定的字符ch,替換指定位置的字符。
public synchronized void setLength(int newLength); // 將給定的字符串,裁剪成指定長度。
(5)獲取子串:
public synchronized String substring(int start); //返回從start位置開始 的所有字符。
public synchronized String substring(int start, int end); //返回 start ~ end-1 位置的所有字符。
(6)字符串反轉:
public synchronized StringBuffer reverse(); //將字符串倒序輸出。
(7)查找:
public int indexOf(String str); //正序查找(從前往后找),返回指定子字符串在此字符串第一次出現的索引,若沒有,則返回-1。
public synchronized int indexOf(String str, int fromIndex); //從 fromIndex 位置開始查找。返回指定子字符串在此字符串第一次出現的索引,若沒有,則返回-1。
public int lastIndexOf(String str); //倒序查找(從后往前找)返回指定子字符串在此字符串第一次出現的索引,若沒有,則返回-1。
public synchronized int lastIndexOf(String str, int fromIndex) ; //從 fromIndex 位置開始查找。返回指定子字符串在此字符串第一次出現的索引,若沒有,則返回-1。
三、String、StringBuffer、StringBuilder的區別
1、String中由final修飾的字符數組來保存字符串,即private final char value[],所以String不可變。
2、StringBuffer與StringBuilder繼承 AbstractStringBuilder類,且其構造方法中調用AbstractStringBuilder類的構造方法,而AbstractStringBuilder類未使用final修飾字符數組來保存字符串,即char value[], 故StringBuilder與StringBuilder可變。
3、線程安全?
(1)String對象不可變,所以是線程安全的。
(2)StringBuffer由於方法加入同步鎖,所以是線程安全的。
(3)StringBuilder沒有同步鎖,所以是線程不安全的。
4、對於String對象,給其賦值分兩種情況:
(1)直接賦值,即String str = "123";時,若常量池中存在"123",則str指向這個字符串,若不存在,則創建一個"123"並置於常量池中,將其引用返回。
(2)使用new關鍵字,即String str = new String("123");如果常量池中沒有"123",則創建"123"並置於常量池中,然后new關鍵字會在堆中創建一個String對象,並將堆中的引用返回。
5、使用情況?
(1)操作少量數據,推薦使用String。
(2)單線程下操作大量數據,推薦使用StringBuilder。
(3)多線程下操作大量數據,推薦使用StringBuffer。
四、java.util.StringTokenizer
1、String類中的split()方法是使用正則表達式分解字符串。使用StringTokenizer類可以不使用正則表達式分解字符串。
2、構造方法:
(1)public StringTokenizer(String str); //為字符串s構造一個分析器,使用默認的分隔標記(空白符),即空格符(多個空格被看成一個空格),換行符,回車符,Tab符等。
(2)public StringTokenizer(String str, String delim); //為字符串s構造一個分析器,使用delim中的字符作為分隔符。
注意:分隔標記的任意組合仍是分隔標記。
3、常用方法:
(1)public int countTokens() ; //獲取當前分析器中按照標記切割后的單詞數量
(2)public String nextToken(); //逐個獲取字符串中分割的單詞,每次成功獲取,則單詞數自動減一。
(3)public boolean hasMoreElements() //控制循環,若當前分析器中還有單詞,即單詞數大於1,則返回true。
1 import java.util.StringTokenizer; 2 3 public class Test { 4 public static void main(String args[]) { 5 StringTokenizer st = new StringTokenizer("hello,,, world", ","); 6 System.out.println("切割后單詞數量為: " + st.countTokens());// 輸出按標記切割后的單詞數 7 System.out.println("單詞分別為: "); 8 while (st.hasMoreTokens()) {// 控制循環 9 System.out.println(st.nextToken()); // 獲取單詞 10 } 11 } 12 } 13 /* 14 * 測試結果: 15 * 切割后單詞數量為: 2 16 * 單詞分別為: 17 * hello 18 * world 19 * 20 */