Day10 API
public static void main(String[] args) {String s1 = "hello";//到常量池里找對象String s2 = new String("hello");s1 = "Tom";s1 = "hello"+"Tom";String s3 = "hello";String s4 = new String("hello");// ==判斷是否為同一對象,判斷內存地址System.out.println(s1==s2);//falseSystem.out.println(s2==s4);//falseSystem.out.println(s1==s3);//falses1 = "hello";System.out.println(s1==s3);//true}
?
?
字符串連接,返回連接后的串
1 String s1 = "hello"; 2 //concat 字符串連接,返回連接后的串 3 s1 = s1.concat("tom"); 4 System.out.println(s1);//hellotom
字符串長度
System.out.println(s1.length());
比較字符序列是否相同,區分大小寫
String s2 = "hellotom"; System.out.println(s1.equals(s2));
比較字符序列是否相同,不區分大小寫
String s3 = "HEllotom"; System.out.println(s1.equalsIgnoreCase(s3));
大寫兼容,全部轉為大寫
System.out.println(s1.toUpperCase());
小寫兼容,全部轉為小寫
System.out.println(s1.toLowerCase());
首次出現的位置索引,沒有返回-1
s1 = "hellohellotom"; System.out.println(s1.indexOf("hello")); System.out.println(s1.indexOf("abc"));
最后一次出現的位置索引
System.out.println(s1.lastIndexOf("hello"));
取出某一個位置的字符
System.out.println(s1.charAt(0));//h
取字符串
//取子串(起始位置)取到最后 System.out.println(s1.substring(10));//tom //[起始位置,終止位置) 不包括終止位置 System.out.println(s1.substring(10, 13));//tom
去除字符串的首尾空格
s1 = " h e l l o t o m "; System.out.println(s1);// h e l l o t o m System.out.println(s1.trim());//h e l l o t o m
字符串替換(舊串,新串)用新串替換舊串
s1 = "hellotom"; System.out.println(s1.replace(s1, "你好"));//你好 //去掉是s1串中的所有空格 s1 = " h e l l o t om "; System.out.println(s1.replace(" ", ""));//hellotom
判斷是否以某個字符串結尾,是true,否false
s1 = "hello.java"; System.out.println(s1.endsWith("java"));//truestartsWith
System.out.println(s1.startsWith("he"));//true
字符串比較大小
s1 = "abc"; //unicode s1在參數之前,返回負數,s1在參數之后,返回正數,相對返回0 System.out.println(s1.compareTo("cc"));//-2 System.out.println(s1.compareTo("aa"));//1 System.out.println(s1.compareTo("abc"));//0
是否包含指定參數的字符串,包含true,不包含false
System.out.println(s1.contains("bc"));//true
把字符串轉換成字符數組
char[] c = s1.toCharArray();//'a','b','c' for(char cc:c) {System.out.print("字符:"+cc);//字符:a字符:b字符:c }
用某個字符串把原始字符串分割為一個字符串數組
s1 = "aa bb cc dd ee"; String[] strs = s1.split(" ");//"aa","bb","cc","dd","ee" for(String ss : strs) {System.out.print("元素:"+ss);//元素:aa元素:bb元素:cc元素:dd元素:ee } 1 public static void main(String[] args) { 2 // String 常用的方法 3 String s1 = "hello"; 4 /* System.out.println(s1);//hello 5 s1 = s1 + "tom";// "hellotom" 6 */ 7 //連接字符串,返回連接后的串 8 s1 = s1.concat("tom");//"hellotom" 9 System.out.println(s1);//"hellotom" 10 //字符串長(字符序列的個數) 11 System.out.println(s1.length());//8 12 //equals比較字符序列是否相同 相同 true 區分大小寫 13 String s2 = "hellotoM"; 14 System.out.println(s1.equals(s2));//fasle 15 //忽略大小寫 16 System.out.println(s1.equalsIgnoreCase(s2)); 17 //大小寫兼容 18 //大寫 19 System.out.println(s1.toUpperCase()); 20 //小寫 21 System.out.println(s1.toLowerCase()); 22 // zhangsan@163.com 23 // 0123------------------------------------ 24 s1 = "hellohellotom"; 25 //位置索引 26 //首次出現的位置索引 ,沒有返回 -1 27 System.out.println(s1.indexOf("hello"));// 0 28 System.out.println(s1.indexOf("@"));//-1 29 //最后一次出現的位置索引 30 System.out.println(s1.lastIndexOf("hello"));//5 31 //取出 某一個 位置 的字符 32 System.out.println(s1.charAt(0));//'h' 33 //取 子串 (起始位置) 取到最后 34 System.out.println(s1.substring(10));//"tom" 35 //[起始位置,終止位置) 不包括終止位置 36 System.out.println(s1.substring(10, 13)); 37 //---------------------------------------- 38 s1 = " h e l l o tom "; 39 //去除字符串 的 首尾 空格 40 System.out.println(s1.trim()); 41 // 42 s1 = "hellotom"; 43 //字符串 替換 (舊串,新串) 用新串 替換 舊串 44 System.out.println(s1.replace("hello","你好"));//"hello"->“你好” 45 s1 = " h e l l o t om "; 46 //問題:去掉 s1串 中的 所有空格 47 System.out.println(s1.replace(" ", "")); 48 // 49 s1 = "hello.java"; 50 //判斷 是否 以 某個 字符串 結尾 是 true 51 System.out.println(s1.endsWith("java"));//true 52 //判斷 是否 以 某個 字符串 開頭 是 true 53 System.out.println(s1.startsWith("he"));//true 54 //字符串 比較大小 55 s1 = "abc"; 56 //unicode s1 在 參數之前 ,返回 負數 ,s1 在參數后 ,返回正數,相等 返回0 57 System.out.println(s1.compareTo("cc"));//-2 58 System.out.println(s1.compareTo("aa"));//1 59 System.out.println(s1.compareTo("abc"));//0 60 //----------------------------------------- 61 //是否 包含 指定參數 的字符串 ,包含 true 62 System.out.println(s1.contains("ab")); 63 //把 字符串 轉換成字符數組 64 char [] c = s1.toCharArray();// 'a','b','c' 65 for(char cc :c) { 66 System.out.println("字符:"+cc); 67 } 68 // 69 s1 = "aa bb cc dd ee"; 70 //用某個 字符串 把原始字符串 分割為 一個 字符串數組 71 String [] strs = s1.split(" ");//"aa","bb","cc","dd","ee" 72 for(String ss:strs) { 73 System.out.println("元素:"+ss); 74 } 75 76 77 78 } View Code如果頻繁更改字符串的值,不要用String類,效率低,用變長字符串類StringBuffer和StringBuilder類
返回容量
StringBuffer sf1 = new StringBuffer(); //具備16個字符的緩沖區 System.out.println(sf1.capacity());//16 StringBuffer sf2 = new StringBuffer("hello"); System.out.println(sf2.capacity());//21=16+5 //直接規定緩沖區大小 StringBuffer sf3 = new StringBuffer(100); System.out.println(sf3.capacity());//100
追加字符串
StringBuffer sf = new StringBuffer(50); sf.append("hello"); System.out.println(sf);//hello char[] c = {'a','b','c'}; sf.append(c,1,2);//bc System.out.println(sf);//hellobc
縮小容量為我存儲的字符的個數大小
System.out.println(sf.capacity());//50 sf.trimToSize(); System.out.println(sf.capacity());//7
向索引位置插入一個字符串
System.out.println(sf);//hellobc sf.insert(5, "你好"); System.out.println(sf);//hello你好bc
修改指定位置的一個字符
sf.setCharAt(5, '您'); System.out.println(sf);//hello您好bc
刪除指定位置索引的一個字符
sf.deleteCharAt(5); System.out.println(sf);//hello好bc
刪除指定范圍的字符串[起始位置,終止位置),不包括終止位置
sf.delete(5, 8); System.out.println(sf);//hello
獲取指定索引處的字符
System.out.println(sf.charAt(1));//e
首次出現的位置索引,沒有返回-1
System.out.println(sf.indexOf("l"));//2
最后一次出現的位置索引
System.out.println(sf.lastIndexOf("l"));//3
反轉
System.out.println(sf);//hello sf.reverse(); System.out.println(sf);//ollehString類與StringBuffer類之間的轉換
String str = sf.toString(); StringBuffer sfr = new StringBuffer(str); 1 public static void main(String[] args) { 2 // StringBuffer StringBuilder 3 //String 不可變類 4 //如果頻繁 更改字符串的值,不要用String效率低,用 變 長字符串類 StringBuffer StringBuilder 5 //StringBuffer 6 StringBuffer sf1 = new StringBuffer(); 7 //具備 16個字符的緩沖區 8 System.out.println(sf1.capacity());//16 9 StringBuffer sf2 = new StringBuffer("hello"); 10 //容量 11 System.out.println(sf2.capacity());//21 12 sf2.append("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); 13 System.out.println(sf2.capacity());// 14 15 StringBuffer sf3 = new StringBuffer(100); 16 System.out.println(sf3.capacity()); 17 18 //----------------StringBuffer-------------------------------- 19 StringBuffer sf = new StringBuffer(50); 20 //容量 21 System.out.println(sf.capacity()); 22 //追加字符串的 23 sf.append("hello"); 24 char[] c = {'a','b','c'}; 25 sf.append(c, 1, 2);//bc 26 System.out.println(sf);//"hellobc" 27 28 System.out.println(sf.capacity()); 29 //縮小容量為 我 存儲的字符的個數大小 30 sf.trimToSize(); 31 System.out.println(sf.capacity()); 32 System.out.println(sf);//"hellobc"// 33 //向 索引 位置 插入一個字符串 34 sf.insert(5, "你好");//"hello你好bc" 35 System.out.println(sf); 36 //修改 指定位置的一個 字符 37 sf.setCharAt(5, '您');//"hello您好bc" 38 System.out.println(sf); 39 //刪除 指定 位置索引的 一個 字符 40 sf.deleteCharAt(5);//"hello好bc" 41 System.out.println(sf); 42 //刪除 指定 范圍 的字符串 【起始位置,終止位置)不包括 終止位置 43 sf.delete(5, 8); 44 System.out.println(sf);//"hello" 45 46 System.out.println(sf.charAt(1));//e 47 System.out.println(sf.indexOf("l"));//2 48 System.out.println(sf.lastIndexOf("l"));//3 49 50 //------------其它---------------------- 51 sf.reverse(); 52 System.out.println(sf); 53 String str = sf.toString();//StringBuffer -> String 54 //String - > StingBuffer 55 StringBuffer sfr = new StringBuffer(str); 56 //----------------------------------------------------- 57 /* 58 * String:定長字符串類,不可變類,表示一個簡單的字符串 用 String 59 * 但是,如果 字符串的值大量更改頻繁更改 ,選擇StringBuffer,StringBuilder 60 * StringBuffer :線程安全的,數據準確,但速度慢 61 * StringBuilder:線程非安全的,多線程環境下數據不準確,但是速度快。 62 */ 63 //-------------------------------------------------- 64 } View Code?
StringBuffer:線程安全的,數據準確,但速度慢
StringBuilder:線程非安全的,多線程環境下數據不準確,但是速度快
正則表達式
?用某種模式去匹配指定字符串的一種表達方式。
?
語法
表達式的模式:Matcher matcher = p.matcher();
驗證:matcher.matches()
public static void main(String[] args) {//正則驗證:郵政編碼必須6位//1.指定正則表達式[0-9]{6}或\\d{6}Pattern p =Pattern.compile("[0-9]{6}");//2.指定要驗證的數據Matcher m = p.matcher("123456");//3.驗證System.out.println(m.matches());//格式驗證true}
作用
2.提供了更加豐富的功能。
?
轉載于:https://www.cnblogs.com/qingyunzong/p/8253134.html
總結
- 上一篇: 实现Runnable接口的好处
- 下一篇: [luoguP1640] [SCOI20