Java 字串- 在本文中,我將向你展示在Java程式設計中, 字串普遍都會使用的7大類14方法
- Java 尋找 (java indexOf, js lastIndexOf)
- Java 擷取 (java substring, java trim)
- Java 對比 (java contains, java startsWith, java endsWith, java matches, java equals)
- Java 組合 (java concat, java join)
- Java 分割(java split)
- Java 換成大小寫 (java toLowerCase, java toUpperCase)
方法 | 例子: |
Java contains 搜查的關键字是否存在於字串中: String.contains(String); String.contains(“要搜查的關键句”); | String str = “I believe I can fly”; System.out.println(str.contains(“believe”)); //true |
Java startsWith 搜查的關键字是否存在於字串的開端: String.startsWith(String); String.startsWith(“要搜查的關键句”); | String str = “I believe I can fly”; System.out.println(str.startsWith(“I”)); //true System.out.println(str.startsWith(“believe”)); //false System.out.println(str.startsWith(“fly”)); //false |
Java endsWith 搜查的關键字是否存在於字串的尾端: String.endsWith(String); String.endsWith(“要搜查的關键句”); | String str = “I believe I can fly”; System.out.println(str.endsWith(“I”)); //false System.out.println(str.endsWith(“believe”)); //false System.out.println(str.endsWith(“fly”)); //true |
Java matches 字串中是否具有一定的Pattern: String.matches(RegxPattern); String.matches(“Regex Pattern”); | String str1 = “I believe I can fly”; String str2 = “www.jprogramstudy.com”; System.out.println(str1.matches(“www(.*)”)); //false System.out.println(str2.matches(“www(.*)”)); //true |
Java substring 撷取字串: String.substring(Int32, Int32); String.substring(由第幾個字元開始撷取, 要撷取多少個); | String str = “I believe I can fly”; System.out.println(str.substring(4, 15)); //lieve I can |
Java split 分割字串: String.split(String); String.split(“要分割的字串”); | String str = “I believe I can fly”; System.out.println(String.join(“,”, str.split(“I”))); //, believe , can fly |
Java trim 移除字串兩邊的空白: String.trim(); String.trim() | String str = ” I believe I can fly “; System.out.println(str.trim()); //I believe I can fly |
Java equals 字串對比 (完全與要對比的字串一樣): String.equals(String); String.equals(“要對比的字串”); | String str1 = “I believe I can fly”; String str2 = “i believe i can fly”; String str3 = “I believe I can fly”; System.out.println(str1.equals(str2)); //false System.out.println(str1.equals(str3)); //true |
Java concat 字串組合: String.concat(String); String.concat(“組合對象字串”); | String str1 = “I believe I can fly! “; String str2 = “Hello World!”; System.out.println(str1.concat(str2)); //I believe I can fly! Hello World! |
Java join 字串數組組合: String.join(String, String[]); String.join(“接合字串”, “字串數組”); | String[] strs = new String[] { “I”, “believe”, “I”, “can”, “fly.” }; System.out.println(String.join(” “, strs)); //I believe I can fly |
Java indexOf 搜查的關键字位於字串中的第幾個字元(由前到後): String.indexOf(String); String.indexOf(“要搜查的關键句”); *若回傳值”-1″, 即是找不到. | String str = “I believe I can fly”; System.out.println(str.indexOf(“I”)); // 0 |
Java lastIndexOf 搜查的關键字位於字串中的第幾個字元 (由後到前): String.lastIndexOf(String); String.lastIndexOf(“要搜查的關键句”); *若回傳值”-1″, 即是找不到. | String str = “I believe I can fly”; System.out.println(str.lastIndexOf(“I”)); // 10 |
Java toLowerCase 將字串轉換成小寫: String.toLowerCase() | String str = “I believe I can fly”; System.out.println(str.toLowerCase()); // i believe i can fly |
Java toUpperCase 將字串轉換成大寫: String.toUpperCase() | String str = “I believe I can fly”; System.out.println(str.toUpperCase()); // I BELIEVE I CAN FLY |
參考資料: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html