這里寫目錄標題
- 創建字串
- 字串參考
- 代碼中的參考
- 比較字串相等
- 代碼一
- 代碼二
- 字串的比較方法
- equals 使用注意事項
- 字串常量池
- 直接賦值
- 采用構造方法
- 字串不可變
- 修改字串
- 字符,位元組與字串
- 字符與字串
- 把字符陣列轉化為字串
- 把字串變成字符
- 判斷字串是否全是數字
- 位元組與字串
- 將位元組變成字串
- 字串轉化為位元組
- 字串常見操作
- 字串比較
- 字串的相等比較
- 字串的大小比較
- 字串查找
- contains()
- indexOf()
- lastIndexOf()
- 判斷是否是某個字符開頭結尾
- 字串的替換
- 全部替換指定字符
- 替換第一個字符
- 字串拆分
- 將字串全部拆分
- 將字串拆分為 n 組
- 一次根據多個分隔符進行分割
- 字串的截取
- 截取到結束
- 截取到某個位置
- 去除字串左右的空格
- 字串大小轉化:
- 把字串全部轉化為大寫
- 把字串全部轉化為小寫
- 字串的拼接
- 判斷字串是否為空
創建字串
我們常用的構造字串的方法有三種:
方法一:直接參考:
String str = "abcdef";
方法二:new 一個字串物件:
String str2 = new String("Hello Bit");
方法三:將字符陣列轉化為字串:
char[] array = {'a', 'b', 'c'};
String str3 = new String(array);
字串參考
注意事項:
- 上面方法一的 abcdef 是一個常量,型別是 String ,str 是對這個字串的參考,
記憶體分布如圖所示:

這里的 String 也是參考型別,
代碼中的參考
對于如下代碼:
String str1 = "Hello";
String str2 = str1;
這段代碼的記憶體分布如圖所示:

要注意的是:String str2 = str1; 并不是 str2 參考了 str1 ,而是指向了和 str1 一樣的地址,所以,修改 str1 的值,str2 的值不受影響:
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = str1;
System.out.println(str1);
System.out.println(str2);
str1 = "Lockey";
System.out.println(str1);
System.out.println(str2);
}
運行結果如下:

因為改變的是 str1 的參考,所以與 str2 無關,
比較字串相等
如果是兩個 int 型別的話,可以直接用 == 比較,但如果是兩個字串型別的話,就有區別了:
代碼一
public static void main(String[] args) {
String str1 = "hello";
String str2 = "hello";
System.out.println(str1 ==str2);
}
運行結果如下:

代碼二
public static void main(String[] args) {
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1 == str2);
}
運行結果如下:

這是因為代碼一和代碼二的記憶體布局不一樣:
代碼一的記憶體布局:

因為字串 Hello 是一個常量字串,所以這里這兩個的指向是同一個,所以是 true ,
代碼二的記憶體布局:

因為是 new 了一個物件,所以就相當于在堆山又開辟了一塊記憶體,所以參考的物件不一樣,這兩個物件僅僅是內容相同,
String 使用 == 比較并不是在比較字串內容, 而是比較兩個參考是否是指向同一個物件,
字串的比較方法
在 Java 當中比較字串的時候只能用 String 提供的 equals 方法,
public static void main(String[] args) {
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1.equals(str2));
}
運行結果如下:

equals 使用注意事項
如果要比較 str 和 Hello 兩個字串是否相等,有兩種寫法:
String str = new String("Hello");
// 寫法一
System.out.println(str.equals("Hello"));
// 寫法二
System.out.println("Hello".equals(str));
這兩種寫法更推薦寫法二,因為如果寫法一的 str 是 null 的話,代碼就會報錯,而方法二就不會報錯,
字串常量池
對 String 類的兩種實體化方法:直接賦值,new 一個新的 String
直接賦值
String str1 = "hello" ;
String str2 = "hello" ;
String str3 = "hello" ;
System.out.println(str1 == str2); // true
System.out.println(str1 == str3); // true
System.out.println(str2 == str3); // true
字串常量池就是里面存放字串常量,從 JDK1.8 開始就把它放在了堆上面,放在常量池里面方便使用,記憶體分布如下圖:

它們參考的都是同一個物件,因為在 JVM 底層會自動維護一個物件池(字串常量池)所以這里的比較也就變成了 true ,
采用構造方法
類物件使用構造方法是標準做法,如下代碼:
String str = new String("Hello");
這里的 Hello 是每次都會創建的物件,當然也可以通過 intern 關鍵字來完成入池操作,如下代碼:
public static void main6(String[] args) {
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1.equals(str2));
}
運行結果如下:

使用入池之后就是這樣:
public static void main(String[] args) {
String str1 = new String("hello").intern() ;
String str2 = "hello" ;
System.out.println(str1 == str2);
}
運行結果如下:

因為都放在了常量池,所以就過就是 true ,
字串不可變
字串是不可改變的,就算使用 + 也只是改變了字串的參考,就像下面這樣:
public static void main(String[] args) {
String str = "hello" ;
str = str + " world" ;
str += "!!!" ;
System.out.println(str);
}
運行結果如下:

但是這段代碼的記憶體圖如下所示:

它的結果只是指向了最后一個字串,
修改字串
如果要修改字串的話,就只能借助原有的字串重新創建字串:
public static void main(String[] args) {
String str1 = "hel";
String str2 = str1+"lllllll";
System.out.println(str2);
}
運行結果如下:

字符,位元組與字串
字符與字串
把字符陣列轉化為字串
通過 new 一個字串完成轉化:
public static void main(String[] args) {
char[] val = {'a','b','c','d','e','f'};
String str = new String(val);
System.out.println(str);
}
運行結果如下:

當然 new 的時候也可以選擇從某個位置到某個位置創建字串:
public static void main(String[] args) {
char[] val = {'a','b','c','d','e','f'};
String str1 = new String(val,1,3);
System.out.println(str1);
}
運行結果如下:

把字串變成字符
public static void main(String[] args) {
String str2 = "hello";
char ch = str2.charAt(2);
System.out.println(ch);
}
這里的 2 是下標 2 ,下標是從 0 開始的,運行結果如下:

當然,也能把整個字串變成字符陣列:
public static void main(String[] args) {
String str2 = "hello";
char[] ch1 = str2.toCharArray();
System.out.println(Arrays.toString(ch1));
}
運行結果如下:

判斷字串是否全是數字
通過將 String 轉化為字符,然后利用 Character 自帶的方法 isDigit() 來判斷:
public static boolean isNumberChar(String s){
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
boolean flag = Character.isDigit(c);
if(flag == false){
return false;
}
}
return true;
}
public static void main(String[] args) {
String str = "1234567890";
System.out.println(isNumberChar(str));
}
運行結果如下:

位元組與字串
將位元組變成字串
還是通過 new 一個字串來完成,而且也可以從某個位置完成字串的轉化:
public static void main(String[] args) {
byte[] bytes = {97,98,99,100};
String str = new String(bytes);
System.out.println(str);
String str2 = new String(bytes,1,3);
System.out.println(str2);
}

字串轉化為位元組
通過將字串轉化為位元組陣列來完成:
public static void main(String[] args) {
String str = "abcdef";
byte[] bytes = str.getBytes();
System.out.println(Arrays.toString(bytes));
}

這里是轉化為編碼,
字符陣列和位元組陣列的區別
- 位元組陣列適合在網路傳輸 資料存盤這樣的場景下使用 更適合針對二進制資料來操作,
- 字符陣列更適合組隊文本資料來操作,尤其是包含中文的時候,
字串常見操作
字串比較
字串的相等比較
通過 String 的 equals 方法來完成對字串的比較:
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = str1;
System.out.println(str1.equals(str2));
}
運行結果如下:

忽略大小寫的字串比較:
public static void main(String[] args) {
String str1 = "abc";
String str2 = "Abc";
System.out.println(str1.equalsIgnoreCase(str2));//equalsIgnoreCase 忽略大小寫的情況
System.out.println(n);
}
運行結果如下:

字串的大小比較
通過對字符的比較從而實作對字串大小的比較:
public static void main(String[] args) {
String str1 = "abc";
String str2 = "Abc";
int n = str1.compareTo(str2);//這里的比較有三種情況:大于0 小于0 等于0
System.out.println(n);
}
運行結果如下:

字串查找
判定一個字串中是否有指定內容存在:
contains()
這里是從左往右查找,找到第一個字符出現的位置,找到為 true 找不到為 false ,
public static void main(String[] args) {
String str = "ababcabcd";
String tmp = "abc";
boolean flag = str.contains(tmp);
System.out.println(flag);
}
運算結果如下:

indexOf()
這個方法是回傳第一次出現的下標位置,也可以再加一個引數,從下標為 n 的位置去尋找:
public static void main(String[] args) {
String str = "ababcabcd";
String tmp = "abc";
int index = str.indexOf(tmp);
System.out.println(index);
}
lastIndexOf()
這里就是從后往前查找,也可以加一個引數從第 n 個下標往前查找,回傳的也是下標:
public static void main(String[] args) {
String str = "ababcabcd";
String tmp = "abc";
int index = str.lastIndexOf(tmp);
System.out.println(index);
}
運行結果如下:

判斷是否是某個字符開頭結尾
判斷某個字符開頭:startsWith() 至少一個引數,用來判斷是否是這個引數開頭的,也可以再加一個引數,表示從下標 n 的位置查找:
public static void main(String[] args) {
String str = "ababcabcd";
String tmp = "abc";
System.out.println(str.startsWith("a",4));
}
運行結果如下:

判斷某個字符結尾:和開頭的方法一樣:
public static void main(String[] args) {
String str = "ababcabcd";
String tmp = "abc";
System.out.println(str.endsWith("cd"));
}
運行結果如下:

字串的替換
全部替換指定字符
使用 replace 方法可以全部替換:
public static void main(String[] args) {
String str = "ababcabcdabcde";
String tmp = str.replace("ab","tp");
System.out.println(tmp);
}
運行結果如下:

替換第一個字符
使用 replaceFirst 方法完成對第一個字符的替換:
public static void main(String[] args) {
String str = "ababcabcdabcde";
String ret = str.replaceFirst("ab","tp");
System.out.println(ret);
}
運行結果如下:

字串拆分
將字串全部拆分
通過 split 方法,傳入根據某個字符拆分,就可以全部拆分了:
public static void main(String[] args) {
String str = "name=zhangsan&age=19";
String[] strings = str.split("&");
for (String s:strings) {
System.out.println(s);
String[] ss = s.split("=");
for (String st:ss) {
System.out.println(st);
}
}
}
這里進行了兩次拆分,第一次是通過 “&” 來拆分,第二次是通過 “=” 來拆分,運行結果如下:

將字串拆分為 n 組
這里也還是通過 String 的 split 方法,不過要加一個引數來說明分為幾組:
public static void main(String[] args) {
String str = "192.168.1.1";
String[] strings = str.split("\\.",3);
for (String s:strings) {
System.out.println(s);
}
}
這里是根據 “.” 來分割,所以就要通過轉義字符 “\” 來完成,后面的引數說明分割為三組,
運行結果如下:

一次根據多個分隔符進行分割
還是通過 split 來進行分割,不過分割的時候要用符號 “|” 來隔開分隔符,
public static void main(String[] args) {
String str = "Ja&va#30 12&21#he#ll&o";
String[] tmp = str.split(" |&|#");
for (String s:tmp) {
System.out.println(s);
}
}
運行結果如下:

字串的截取
截取到結束
通過 String 的方法 substring() 方法來完成對字串的提取,給定一個下標,從下標出提取:
public static void main(String[] args) {
String str = "abcdefg";
String tmp = str.substring(2);
System.out.println(tmp);
}
運行結果如下:

截取到某個位置
還是通過 substring() 方法,不過要再加一個引數 n 用來表示截取到某個位置:
public static void main(String[] args) {
String str = "abcdefg";
String tmp1 = str.substring(2,5);
System.out.println(tmp1);
}
運行結果如下:

去除字串左右的空格
通過方法 trim 來完成對空格的去除:
public static void main(String[] args) {
String str = " abc def ";
String tmp = str.trim();
System.out.println(tmp);
}
運行結果如下:

字串大小轉化:
把字串全部轉化為大寫
通過 String 方法 toUpperCase() 完成轉化:
public static void main15(String[] args) {
String str = "abcdefBFEG";
String ret = str.toUpperCase();
System.out.println(ret);
}
運行結果如下:

把字串全部轉化為小寫
也是通過方法 toLowerCase() 來完成轉化:
public static void main(String[] args) {
String str = "abcdefBFEG";
String tmp = str.toLowerCase();
System.out.println(tmp);
}
運行結果如下:

字串的拼接
通過 String 的方法 concat 來完成拼接:
public static void main(String[] args) {
String str = "abcd";
String ret = str.concat("ef");
System.out.println(ret);
}
運行結果如下:

要注意的是,這里拼接之后的字符不入池,
判斷字串是否為空
這里也是通過 String 的方法 isEmpty() 來完成判斷:
public static void main(String[] args) {
String str = "abcd";
System.out.println(str.isEmpty());
}
運行結果如下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/413406.html
標籤:java
