JavaS學習筆記 - String類
阿新 • • 發佈:2020-08-27
String
- 字串一旦建立就不可改變
- 帶有雙引號的字串在字串常量池中儲存
- 字串比較時需要使用 equals 方法,String 類重寫了 equals 和 toString 方法
建立字串
public class Main { public static void main(String[] args) { String str = "abc"; String string = new String("abc"); String string1 = new String("bcd"); } }
記憶體圖分析
拼接
public class Main {
public static void main(String[] args) {
String str = "abcdefg";
String string = "abcdefg" + " hello";
System.out.println(string);//abcdefg hello
}
}
- 如果想要修改字串的某一個字元,是不支援對某一個位置的字元修改的,這也是字串的不可變性,如果需要進行修改就必須擷取一部分字元,然後通過 + 進行拼接,這樣相當於新生成了一個字串,字串的不可變性是指你不能修改str中的單個字元,但是你可以讓str指向另外一個引用,不可變字串優點:可以讓字串共享。
記憶體圖分析
常用方法
子串
public class Main {
public static void main(String[] args) {
String str = "abcdefg";
String string = str.substring(0, 3);
System.out.println(string);
}
}
endsWith:判斷字串是否以指定的字尾結束
startsWith:判斷字串是否以指定的字首開始
charAt:返回指定位置的字元
compareTo:按照字典序比較兩個字串
contains
equals:判斷兩個字串是否相等,不忽略大小寫。JDK 8 底層轉換為 char 進行比較,未使用 comparTo 方法。
equalsIgnoreCase:判斷兩個字串是否相等,忽略大小寫
indexOf:返回指定字串第一次出現的位置
String str = "aabcabc";
System.out.println(str.indexOf("abc"));//1
lastIndexOf:返回指定字串最後一次出現的位置
empty:判斷字串是否為空
length:返回字串的長度
replace:字串替換
public class Main {
public static void main(String[] args) {
String str = "aabcabc";
str = str.replace("a", "-");
System.out.println(str);//--bc-bc
}
}
trim:去除字串的首位空格
toLowerCase:字串變小寫
toUpperCase:字串變大寫
split:字串分割
public class Main {
public static void main(String[] args) {
String str = "aa.bc.abc";
String[] strings = str.split("\\.");
for (String string : strings) {
System.out.print(string + " ");//aa bc abc
}
}
}
valueOf:靜態方法,將其他型別轉換為字串
空串和null
- null 是指某一個物件沒有執行任何一個地址,空引用,使用這個物件將發生空指標異常(引用型別預設值為 null)
- 空串是指這個物件指向了一個地址,是一個有效的地址,只不過這個地址上儲存的字串長度為 0
StringBuffer
- StringBuffer 底層是可變的 char 陣列,所以字串可以變化,但是 String 底層的 char 陣列被 final 修飾,一旦指向了某一個引用,那麼這個陣列將不能在進行修改。
- StringBuffer 預設初始化容量為 16
public StringBuffer() {
super(16);
}
- 我們在使用 String 拼接字串的時候,中間會產生大量的空間浪費,字串常量池中會產生很多字串物件(參考上述記憶體分析圖),但是如果我們使用 StringBuffer 就可以避免這種空間浪費。
public AbstractStringBufferappend(String str) {
if (str == null)
return appendNull();
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
//擴容
private void ensureCapacityInternal(int minimumCapacity) {
// overflow-conscious code
if (minimumCapacity - value.length > 0) {
value = Arrays.copyOf(value,
newCapacity(minimumCapacity));
}
}
//在進行擴容的時候,將原來的char陣列拷貝到一個新的char陣列中
//原來的char陣列將會被垃圾回收器回收這樣就實現了擴容
//相比於String的拼接省了很多空間
public static char[] copyOf(char[] original, int newLength) {
char[] copy = new char[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
- 如果在建立字串的時候就指定了容量,這個就提高了程式的執行效率,因為底層可以不用進行擴容,省去了擴容所浪費的時間。
public class Main {
public static void main(String[] args) {
StringBuffer str = new StringBuffer(100);
str.append("a");
str.append("b");
}
}
insert
- 在使用的時候需要注意陣列越界的問題
public class Main {
public static void main(String[] args) {
StringBuffer str = new StringBuffer(100);
str.append("a");
str.append("b");
str.insert(0, "---");
System.out.println(str);//---ab
}
}
delete
public class Main {
public static void main(String[] args) {
StringBuffer str = new StringBuffer(100);
str.append("abcd");
str.append("b");
System.out.println(str);//abcdb
str.delete(0, 1);
System.out.println(str);//bcdb
str.deleteCharAt(3);
System.out.println(str);//bcd
}
}
replace
public class Main {
public static void main(String[] args) {
StringBuffer str = new StringBuffer(100);
str.append("abcd");
str.append("b");
System.out.println(str);//abcdb
str.replace(0, 3, "-");//-db
System.out.println(str);
}
}
toString
public class Main {
public static void main(String[] args) {
StringBuffer str = new StringBuffer(100);
str.append("abcd");
str.append("b");
System.out.println(str);//abcdb
String string = str.toString();
System.out.println(string);//abcdb
}
}
reverse
public class Main {
public static void main(String[] args) {
StringBuffer str = new StringBuffer(100);
str.append("abcd");
str.append("b");
System.out.println(str);//abcdb
str.reverse();
System.out.println(str);//bdcba
}
}
StringBuilder
- StringBuffer 是 StringBuilder 的前身,StringBuffer 中的方法都有 synchronized 關鍵字來修飾,StringBuffer 是執行緒安全的,但是他的效率比較低,允許採用多執行緒的方式新增或者刪除字串。StringBuilder 是非執行緒安全的,如果所有的程式都在單個執行緒的環境下執行,那麼我們就可以使用 StringBuilder。