Java基礎 第二節 第七課
String 類
概述
特點
使用步驟
常用方法
判斷功能的方法
獲取功能的方法
轉換功能的方法
分割功能的方法
String 類的練習
拼接字符串
統計字符個數
概述
java.lang.String類代表字符串. Java 程序中所有的字符串文字 ( 例如: “abc” ) 都可以被看作是現實此類的實例.
類 String 中包括用于檢查各個字符串的方法, 比如用于比較字符串, 搜索字符串, 提取子字符串以及創建具有翻譯的大寫或小寫的所有字符的字符串的副本.
特點
字符串不變: 字符串的值在創建后不能被更改.
String s1 = "abc"; s1 += "d"; System.out.println(s1); // "abcd" // 內存中有"abc", "abcd" 兩個對象, s1 從指向" abc", 改變指向, 指向了 "abcd"
1
2
3
4
因為 String 對象是不可變的, 所以它們可以被共享
String s1 = "abc"; String s2 = "abc"; // 內存中只有一個 "abc" 對象被創建, 同時被 s1 和 s2 共享
1
2
3
“abc” 等效于char[] data={ 'a' , 'b' , 'c' }.
例如: String str = "abc"; 相當于: char data[] = {'a', 'b', 'c'}; String str = new String(data); // String底層是靠字符數組實現的
1
2
3
4
5
6
7
使用步驟
查看類:
java.lang.String: 此類不需要導入
查看構造方法:
public String(): 初始化新創建的 String 對象, 以使其表示空字符串序列
public String(char[] value): 通過當前參數中的字符數組來構造新的 String
public String(byte[] bytes): 通過使用平臺的默認字符集解碼當前參數中的字節數組來構造新的 String
構造舉例, 代碼如下:
// 無參構造 String str = new String(); // 通過字符數組構造 char chars[] = {'a', 'b', 'c'}; String str2 = new String(chars); // 通過字節數組構造 byte bytes[] = { 97, 98, 99 }; String str3 = new String(bytes);
1
2
3
4
5
6
7
8
9
10
常用方法
判斷功能的方法
public boolean equals (Object anObject): 將此字符串與指定對象進行比較
public boolean equalsIgnoreCase (String anotherString): 將此字符串與指定對象進行比較, 忽略大小寫.
方法演示, 代碼如下:
public class Test65 { public static void main(String[] args) { // 創建字符串對象 String s1 = "hello"; String s2 = "hello"; String s3 = "Hello"; // boolean equals(Object obj): 比較字符串的內容是否相同 System.out.println(s1.equals(s2)); // true System.out.println(s1.equals(s3)); // false // boolean equalsIgnoreCase(String str): // 比較字符串的內容是否相同, 忽略大小寫 System.out.println(s1.equalsIgnoreCase(s2)); // true System.out.println(s1.equalsIgnoreCase(s3)); // ture } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
注: Object 是 “對象” 的意思, 也是一種引用類型. 作為參數類型, 表示任意對象都可以傳遞到方法中.
獲取功能的方法
public int length (): 返回此字符串的長度
public String concat(String str): 將指定的字符串連接到該字符創的末尾
public char charAt(int index): 返回指定索引處的 char 值
public int indexOf(String str): 返回指定子字符串第一次出現在該字符串內的索引
public String substring(int beginIndex): 返回一個子字符串, 從 beginIndex 開始截取字符串到字符串結尾
public String substring (int beginIndex, int endIndex): 返回一個子字符串, 從 beginIndex 到 endIndex 截取字符串. 含 beginIndex, 不含 endIndex.
方法演示, 代碼如下:
public class Test66 { public static void main(String[] args) { // 創建字符串對象 String s1 = "helloworld"; // int length(): 獲取字符串的長度, 其實也就是字符個數 System.out.println(s1.length()); // 輸出結果: 10 // String concat(String str): 將指定的字符串連接到該字符串的末尾 String s2 = "helloworld"; String s3 = s2.concat("**iamlittlewhite"); System.out.println(s3); // 輸出結果: helloworld**iamlittlewhite // char charAt(int index): 獲取指定索引處的字符 System.out.println(s1.charAt(0)); // 輸出結果: h System.out.println(s1.charAt(1)); // 輸出結果: e // String indexOf(String str): 獲取str在字符串對象中第一次出現的索引, 沒有返回-1 System.out.println(s1.indexOf("1")); // 輸出結果: -1 System.out.println(s1.indexOf("owo")); // 輸出結果: 4 System.out.println(s1.indexOf("ak")); // 輸出結果: -1 // String substring(int start): 從start開始截取字符串代字符串結尾 System.out.println(s1.substring(0)); // 輸出結果: helloworld System.out.println(s1.substring(5)); // 輸出結果: world // String substring(int start,int end): 從start到end截取字符串. 含start, 不含end System.out.println(s1.substring(0, s1.length())); // 輸出結果: helloworld System.out.println(s1.substring(3,8)); // 輸出結果: lowor } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
轉換功能的方法
public char[] toCharArray (): 將此字符串轉換為新的字符數組.
public byte[] getBytes (): 使用平臺的默認字符集將該 String 編碼轉換為新的字節數組.
public String replace (CharSequence target, CharSequence replacement): 將于 target 匹配的字符串使用 replacement 字符串替換.
方法演示, 代碼如下:
public class Test67 { public static void main(String[] args) { // 創建字符串對象 String s = "abcde"; // char[] toCharArray(): 把字符串轉換為字符數組 char[] chs = s.toCharArray(); for (int i = 0; i < chs.length; i++) { System.out.println(chs[i]); } // byte[] getBytes(): 把字符串轉換為字節數組 byte[] bytes = s.getBytes(); for (int i = 0; i < bytes.length; i++) { System.out.println(bytes[i]); } // 替換字母it為大寫IT String str = "iamlittlewhite"; String replace = str.replace("little","big"); System.out.println(replace); // 輸出結果: iambigwhite } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
注: CharSequence 是一個接口, 也是一種引用類型. 作為參數類型, 可以把 String 對象傳遞到方法中.
分割功能的方法
public String[] split(String regex): 將此字符串安裝給定的 regex (規則) 拆分為字符串數組.
方法演示, 代碼如下:
public class Test68 { public static void main(String[] args) { // 創建字符串對象 String s = "aa bb cc"; String[] strArray = s.split(" "); // 輸出結果: ["aa","bb","cc"] for(int x = 0; x < strArray.length; x++) { System.out.println(strArray[x]); // 輸出結果: aa bb cc } } }
1
2
3
4
5
6
7
8
9
10
String 類的練習
拼接字符串
定義一個方法, 把數組 {1,2,3} 按照指定格式拼接成一個字符串. 格式參照如下: [word1#word2#word3].
public class Test69 { public static void main(String[] args) { // 定義一個int類型的數組 int[] arr = {1, 2, 3}; // 調用方法 String s = arrayToString(arr); // 輸出結果 System.out.println("s:" + s); } /** * 寫方法實現把數組中的元素按照指定的格式拼接成一個字符串 * @param arr 傳入數組 * @return 返回拼接字符串 */ public static String arrayToString(int[] arr) { // 創建字符串s String s = new String("["); // 遍歷數組,并拼接字符串 for (int x = 0; x < arr.length; x++) { if (x == arr.length - 1) { s = s.concat(arr[x] + "]"); } else { s = s.concat(arr[x] + "#"); } } return s; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
統計字符個數
import java.util.Scanner; public class Test70 { public static void main(String[] args) { // 鍵盤錄入一個字符串數據 Scanner sc = new Scanner(System.in); System.out.println("請輸入一個字符串數據:"); String s = sc.nextLine(); // 定義三個統計變量,初始化值都是0 int bigCount = 0; int smallCount = 0; int numberCount = 0; // 遍歷字符串,得到每一個字符 for(int x=0; x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Java 數據結構
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。