charAt indexOf length startsWith substring 轉換大小寫 trim valueOf replaceAll split matches StringBuilder
String類介紹
/** * String是不變對象,即: * 字符串對象創建后,表示的字符內容不可變,若 * 改變必定創建新對象. * @author ta * */ public class StringDemo { public static void main(String[] args) { /* * java對於字符串有一個優化,即: * 字符串常量池,這是在堆內存中開辟的 * 一塊空間,用於保存所有使用字面量形式 * 創建的字符串對象,當再次使用該字面量 * 創建新的字符串時會直接重用而不會再 * 創建新的來節省內存開銷. */ String s1 = "123abc"; //s2會重用s1創建的字符串對象 String s2 = "123abc"; String s3 = "123abc"; System.out.println(s1==s2);//true System.out.println(s1==s3);//true //修改內容會創建新對象 s1 = s1 + "!";//s1不再指向原對象 System.out.println("s1:"+s1);//123abc! System.out.println("s2:"+s2);//123abc System.out.println(s1==s2);//false System.out.println(s2==s3);//true /* * new關鍵字很強制,一定會創建新對象 */ String s4 = new String("123abc"); System.out.println("s4:"+s4); /* * s2與s4是兩個不同的字符串對象,雖然 * 內容都是"123abc",但是兩個變量保存 * 的地址不同. */ System.out.println(s4==s2);//false /* * 通常我們比較字符串都是比較字符串的 * 內容,因此比較字符串時我們不會使用 * "==",而是使用字符串的方法equals. * equals方法是用來比較兩個字符串對象 * 所表示的內容是否相同的. */ boolean tf = s2.equals(s4); System.out.println(tf);//true /* * 這里用到了編譯器的一個特性: * 編譯器在編譯源代碼時,當遇到一個計算 * 表達式可以在編譯期間確定結果時就會 * 直接進行計算,並將結果編譯到class文件 * 中,這樣JVM每次運行程序時就無需再計算. * 比如: int a = 1+1; * 編譯器編譯后class改為:int a = 2; * 下面的字符串也是如此,會被改為: * String s5 = "123abc"; */ String s5 = "123"+"abc"; System.out.println("s5:"+s5); System.out.println(s2==s5);//true String s = "123"; String s6 = s + "abc"; System.out.println("s6:"+s6); System.out.println(s2==s6);//false } }
1.charAt
/** * char charAt(int index) * 獲取當前字符串中指定位置對應的字符 * @author ta * */ public class CharAtDemo { public static void main(String[] args) { // 0123456789012345 String str = "thinking in java"; char c = str.charAt(9); System.out.println(c); //判斷回文 0 1 2 3 4 5 6 7 8 String line = "上海自來水來自海上"; for(int i=0;i<line.length()/2;i++) { char c1 = line.charAt(i); char c2 = line.charAt(line.length()-1-i); if(c1!=c2) { System.out.println("不是回文!"); /* * 當方法返回值類型為void時,return是 * 可以單獨使用的,目的是結束方法 */ return; } } System.out.println("是回文!"); } }
2.indexOf
/** * int indexOf(String str) * 查找給定字符串在當前字符串中的位置,若當前 * 字符串不包含給定內容則返回值為-1. * @author ta * */ public class IndexOfDemo { public static void main(String[] args) { // 0123456789012345 String str = "thinking in java"; int index = str.indexOf("in"); System.out.println(index);//2 /* * 重載的方法允許我們從指定位置開始 * 查找第一次出現指定字符的位置 */ index = str.indexOf("in", 3); System.out.println(index);//5 //查找最后一次出現指定字符串的位置 index = str.lastIndexOf("in"); System.out.println(index);//9 } }
3.length
/** * int length() * 獲取當前字符串的長度(字符個數) * @author ta * */ public class LengthDemo { public static void main(String[] args) { String str = "我愛java!"; int len = str.length(); System.out.println(len); } }
4.startsWith
/** * boolean startsWith(String str) * boolean endsWith(String str) * 判斷字符串是否是以給定的字符串開始或結尾的 * @author ta * */ public class StartsWithDemo { public static void main(String[] args) { String str = "thinking in java"; boolean starts = str.startsWith("thi"); System.out.println(starts); boolean ends = str.endsWith("ava"); System.out.println(ends); } }
5.substring
/** * String substring(int start,int end) * 截取當前字符串中指定范圍內的字符串. * * 注:java API中,通常使用兩個數字表示范圍時, * 都是含頭不含尾的. * @author ta * */ public class SubstringDemo { public static void main(String[] args) { // 01234567890 String line = "www.eeee.cn"; String sub = line.substring(4,8); System.out.println(sub); //從指定位置開始截取到字符串末尾 sub = line.substring(4); System.out.println(sub); } }
6.轉換大小寫
/** * String toUpperCase() * String toLowerCase() * 將當前字符串中的英文部分轉換為全大寫或全小寫 * @author ta * */ public class ToUpperCaseDemo { public static void main(String[] args) { String str = "我愛Java"; String upper = str.toUpperCase(); System.out.println(upper); String lower = str.toLowerCase(); System.out.println(lower); } }
7.trim
/** * String trim() * 去除當前字符串兩側的空白字符 * @author ta * */ public class TrimDemo { public static void main(String[] args) { String str = " hello "; String trim = str.trim(); System.out.println(str); System.out.println(trim); } }
8.valueOf
/** * 字符串提供了若干的重載的valueOf方法,它們 * 都是靜態方法. * static String valueOf(XXX xxx) * 作用是將給定的內容轉換為字符串 * * @author ta * */ public class ValueOfDemo { public static void main(String[] args) { int a = 123; //將int值轉換為String String str = String.valueOf(a); System.out.println(str); double dou = 123.123; String str2 = String.valueOf(dou); System.out.println(str2); //任何類型與字符串連接結果都是字符串 str = a+""; System.out.println(str); } }
9.replaceAll
/** * 和諧用語 * @author ta * */ public class Demo2 { public static void main(String[] args) { String regex = "(wqnmlgb|dsb|cnm|nc|nmbwcnm|mdzz|djb)"; String message = "nmbwcnm!你是nc么?你個dsb,你就是一個djb!"; message = message.replaceAll(regex, "***"); System.out.println(message); } }
/** * String支持正則表達式方法三: * String replaceAll(String regex,String str) * 將當前字符串中滿足正則表達式的部分替換為 * 給定的內容 * @author ta * */ public class ReplaceAllDemo { public static void main(String[] args) { String line = "abc123def456ghi789jkl"; /* * 將字符串中的數字部分替換為"#NUMBER#" */ line = line.replaceAll("[0-9]+", "#NUMBER#"); System.out.println(line); } }
10.split
import java.util.Arrays; /** * String支持正則表達式方法二: * String[] split(String regex) * 將當前字符串中按照滿足正則表達式的部分拆分 * 然后將拆分后的字符串以數組形式返回. * @author ta * */ public class SplitDemo { public static void main(String[] args) { String line = "abc123def456ghi789jkl"; //按照數字部分拆分字符串 // String[] data = line.split("[0-9]+"); /* * 如果拆分過程中連續匹配到兩次可拆分的 * 內容時,中間會拆分出一個空字符串. * 但是如果是在字符串末尾連續匹配上則所有 * 拆分出的空字符串會被忽略. */ String[] data = line.split("[0-9]"); System.out.println(data.length); System.out.println(Arrays.toString(data)); } }
11.matches(正則表達式是否匹配)
/** * String支持正則表達式的方法之一: * boolean matches(String regex) * 使用給定的正則表達式匹配當前字符串是否符合 * 格式要求,符合則返回true. * * 注意:給定的正則表達式就算不指定邊界匹配符 * 即:(^...$)也是做完全匹配驗證的. * @author ta * */ public class MatchesDemo { public static void main(String[] args) { String email = "fancq@gggg.cn"; /* * [a-zA-Z0-9_]+@[a-zA-Z0-9]+(\.[a-zA-Z]+)+ * */ String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+"; boolean match = email.matches(regex); if(match) { System.out.println("是郵箱"); }else { System.out.println("不是郵箱"); } } }
12.StringBuilder
/** * String的優化措施僅照顧重用性,因此頻繁修改 * 字符串會帶來內存開銷大,運行效率差的結果. * 對此,java提供了一個專門用於修改字符串的API * java.lang.StringBuilder * 其內部維護一個可變的char數組,所有的修改都是 * 在這個數組中進行的,因此開銷小,性能好. * 並且其提供了便於修改字符串的一系列方法,包 * 括了:增,刪,改,插等基本操作. * @author ta * */ public class StringBuilderDemo { public static void main(String[] args) { String str = "好好學習java"; StringBuilder builder = new StringBuilder(str); /* * 好好學習java * 好好學習java,為了找個好工作! */ builder.append(",為了找個好工作!"); System.out.println(str); //獲取StringBuilder內部的字符串 str = builder.toString(); System.out.println(str); /* * 好好學習java,為了找個好工作! * 好好學習java,就是為了改變世界! */ builder.replace(9, 16, "就是為了改變世界"); System.out.println(builder); /* * 好好學習java,就是為了改變世界! * ,就是為了改變世界! */ builder.delete(0, 8); System.out.println(builder); /* * ,就是為了改變世界! * 活着,就是為了改變世界! */ builder.insert(0, "活着"); System.out.println(builder); } }