java.lang.Stringjava
String即字符串,建立以後不可更改(能夠指向其餘String)數組
這裏要分清楚空字符串""和空null的區別,空字符串""是一個對象,只是沒有存儲字符串;null表示什麼也沒有(連對象也沒有)安全
public String() //空構造 public String(byte[] bytes) //將byte轉化爲String public String(byte[] bytes,Charset charset) //使用指定字符集把字節數組轉成字符串 public String(byte[] bytes,int index,int length) //將字節數組的一部分轉成字符串 public String(char[] value) //將字符數組轉成字符串 public String(char[] value,int index,int count) //將字符數組的一部分轉成字符串 public String(String original)
String s1 = "abc"; //s1指向一個字符串字面值,而字符串字面值位於字符串常量池中,字符串常量池位於方法區中 String s2 = "abc"; //相同的字符串字面值在字符串常量池中只有一份,也就是說s1,s2指向字符串常量池中的同一個字符串字面值 System.out.println(s1==s2); //s1,s2指向同一個對象 System.out.println(s1.equals(s2)); //s1,s2指向對象內容相同
String s1 = "ab"; String s2 = "abc"; String s3 = s1 + "c"; System.out.println(s3 == s2); //false,字符串間串聯是經過Stringbuffer或Stringbuilder類的append方法實現的,而後經過toString轉化爲String,期間生成了Stringbuffer和String兩個對象,最後賦值給目標String //至關於 (new StringBuilder()).append(s1).append(s2).toString(),String不可更改仍然成立 System.out.println(s3.equals(s2));//true
boolean equals(Object obj) //比較字符串的內容是否相同,區分大小寫 boolean equalsIgnoreCase(String str) //比較字符串的內容是否相同,忽略大小寫 boolean contains(String str) //判斷大字符串中是否包含小字符串 boolean startsWith(String str) //判斷字符串是否以某個指定的字符串開頭 boolean endsWith(String str) //判斷字符串是否以某個指定的字符串結尾 boolean isEmpty() //判斷字符串是否爲空。
int length() //獲取字符串的長度。 char charAt(int index) //獲取指定索引位置的字符 int indexOf(int ch) //返回指定字符在此字符串中第一次出現處的索引。 int indexOf(String str) //返回指定字符串在此字符串中第一次出現處的索引。 int indexOf(int ch,int fromIndex) //返回指定字符在此字符串中從指定位置後第一次出現處的索引。 int lastIndexOf(String str,int fromIndex) //返回指定字符串在此字符串中從指定位置後第一次出現處的索引。 String substring(int start) //從指定位置開始截取字符串,默認到末尾。 String substring(int start,int end) //從指定位置開始到指定位置結束截取字符串。
byte[] getBytes() //把字符串轉換爲字節數組。 char[] toCharArray() //把字符串轉換爲字符數組。 static String valueOf(char[] chs) //把字符數組轉成字符串。 static String valueOf(int i) //把int類型的數據轉成字符串。注意:String類的valueOf方法能夠把任意類型的數據轉成字符串。 String toLowerCase() //把字符串轉成小寫。 String toUpperCase() //把字符串轉成大寫。 String concat(String str) //把字符串拼接。
String replace(char old,char new) //替換單個字符 String replace(String old,String new) //替換字符串 String trim() //剔除兩側空格 int compareTo(String str) //字典順序比較大小 int compareToIgnoreCase(String str) //字典順序比較大小(忽略大小寫)
java.lang.StringBufferapp
StringBuffer是線程安全的可變字符序列,相應的效率較低函數
public StringBuffer() //無參構造方法,默認容量爲16 public StringBuffer(int capacity) //指定容量的字符串緩衝區對象 public StringBuffer(String str) //指定字符串內容的字符串緩衝區對象
public int capacity() //查看容量 public int length() //查看長度
public StringBuffer append(String str) //能夠把任意類型數據添加到字符串緩衝區裏面,並返回字符串緩衝區自己 public StringBuffer insert(int offset,String str) //在指定位置把任意類型的數據插入到字符串緩衝區裏面,並返回字符串緩衝區自己 public StringBuffer deleteCharAt(int index) //刪除指定位置的字符,並返回自己 public StringBuffer delete(int start,int end) //刪除從指定位置開始指定位置結束的內容,並返回自己 public StringBuffer replace(int start,int end,String str) //從start開始到end用str替換 public StringBuffer reverse() //字符串反轉 public String substring(int start,int end) //截取從指定位置開始到結束位置,包括開始位置,不包括結束位置
java.lang.StringBuilderui
StringBuilder是一個線程非安全的可變序列,效率高線程
方法和StringBuffer基本一致,這裏就不過多贅述code
類 | 線程安全 | 效率高 | 可變 |
---|---|---|---|
String | 否 | 是 | 否 |
StringBuffer | 是 | 否 | 是 |
StringBuilder | 否 | 是 | 是 |