JavaSE的String類
目錄
String類的兩種例項化方式
1.直接賦值(在堆上分配空間)---常用
String str = "hello";
2.傳統方法--通過構造方法例項化String物件(會產生一塊垃圾空間)----不推薦
String str = new String("hello");
String採用共享設計模式
JVM底層會自動維護一個字串的物件池(就是一個物件陣列:目的為了減小開銷),如果現在採用直接賦值的形式進行String的物件例項化,該物件會自動儲存在這個物件池中。下次繼續使用直接賦值的模式宣告String物件,此時物件池中若有指定內容,則直接引用;如果沒有,則開闢新的堆空間後將其儲存在物件池中供下次使用。
String str1 = "hello"; String str2 = "hello"; String str3 = "hello";
手工入池(本地方法):public native String intern( );
String str1 = "hello";
String str2 = new String("hello").intern( );
//使用String構造方法會開闢一塊新的空間存hello,這樣有一塊成為垃圾空間,也會對字串共享產生問題,
可以採用手工入池的方法,解決這一問題
System.out.println(str1 == str2);//true
字串常量不可變更。字串一旦定義後不可改變。開闢新的堆空間,棧的指向在變..產生垃圾空間
原則:字串別改變太多,不超過三次。
字元與字串的相互轉換(**)
1.將字元陣列-->字串
public String (char[ ] value) //例 String str = new String(data);
public String (char[ ] value , int offset , int count) //offset起始座標 count個數
2.將字串-->單個字元
public char charAt(int index)
例 "hello".charAt(5)
3.將字串-->字元陣列
public char[ ] toCharArray( );
例 char[ ] data = "hello".toCharArray( );
4.取得字串長度
public int length( );
(※)判斷一個字串是否由數字組成?
public static boolean is Number(String str){
char[] data = str.toCharArray();
for(int i = 0; i<data.length; i++){
if(data[i] < '0' || data[i] > '9') {
return false;
} else{
return ture;
}
}
}
位元組與字串的相互轉換
1.將位元組陣列byte[ ]-->字串 -----網路傳輸使用
public String (char[ ] value) //例 String str = new String(data);
public String (char[ ] value , int offset , int count) //起始座標 個數
2.將字串-->位元組陣列(**)
public byte[ ] getBytes( );
3.將字串按照指定編碼-->位元組陣列
public byte[ ] getBytes(String charsetName);
字串相等比較(比較內容,而不是地址)
public boolean equals(String anotherString)
例 str1.equals(str2)
解釋"=="和"equals"的區別:前者比較的是兩個物件所儲存的記憶體地址數值,後者是兩個字串的內容。
字串常量("...")是String的匿名物件。
public static void main(String[] args) {
String str = null;
System.out.println("hello".equals(str));//推薦寫法
System.out.println(str.equals("hello"));
}
//執行結果↓↓↓
Tips:開發中,如果要判斷使用者輸入的字串是否等同於特定字串,一定要將String常量寫在前面,避免出現NullPointerException空指標異常。
字串比較
1.區分大小寫相等比較
public boolean equals(String anotherString)
2.不區分大小寫相等比較
public boolean equalsIgnoreCase(String anotherString)
3.比較兩個字串大小
public int compareTo (String anotherString)
返回大於0:表示大於比較物件
返回等於0:表示兩者相等
返回小於0:表示小於比較物件
字串查詢(**)
public boolean contains(String str):判斷str在本字串中是否存在。
public boolean startsWith(String str):判斷是否以指定字串開頭
public boolean startsWith(String str,int index):從指定位置開始判斷是否以指定字串開頭
public boolean endsWith(String str):判斷是否以指定字串結尾
indexOf(String str) :不存在返回-1 存在返回第一次出現的下標
字串替換
public String replaceAll(String regex,String replacement):替換所有指定內容
public String replaceFirst(String regex,String replacement):替換首個內容
正則表示式
字串拆分
public String[] split(String regex):將字串按照指定格式全部拆分
public String[] split(String regex,int limit):將字串拆分,陣列長度為limit
注意:\\. 有特殊含義的字元需要強轉
字串擷取
public String substring(int beginIndex):從指定位置擷取到字串結尾
public String substring(int beginIndex,int endIndex):擷取部分內容(左閉右開)
String類的其他方法
1.去掉左右空格
public String trim();
2.字串轉大小寫
public String toUpperCase();
public String toLowerCase();
3.判斷字串是否為空(只能判斷是否為空字串,不包含null)
public boolean isEmpty();
StringBuffer \ StringBuilder
方便進行字串的修改,在java.lang包中
1.字串修改
public StringBuffer append(各種資料型別)
StringBuffer sb = new StringBuffer();
sb.append ("hello").append(10); //將hello 和 10 拼在一起
String->StringBuffer
呼叫StringBuffer的構造方法或append();
StringBuffer->String
StringBuffer.toString();
2.字串反轉
public StringBuffer reverse();
3.刪除指定範圍的資料(因為在一塊堆空間)
public StringBuffer delete(int start,int end);
4.插入資料
public StringBuffer insert(int offset,各種資料型別);
5.兩者區別
String 的內容不可修改,而這兩隻sb可以改內容(append)
StringBuffer 採用同步處理,執行緒安全,效率較低
StringBuilder 採用同步處理,執行緒不安全,效率較高,String."+"底層會將String->StringBuilder