淺談Java中String的常用方法
String中常用的方法,我以代碼的形式,來說明這些常用的方法。
@Test public void test1(){//1.返回字符串的長度String s1 = 'helloworld';System.out.println(s1.length());//2.返回某索引處的字符System.out.println(s1.charAt(1));//3.判斷字符串是否是空字符串System.out.println(s1.isEmpty());//4.將String中的所有字符串轉換成小寫String s2 = 'ShoPPing';String s3 = s2.toLowerCase();System.out.println(s3);//5.將String中的所有字符串轉換成大寫String s4 = s2.toUpperCase();System.out.println(s4);//6.返回字符串的副本,忽略前導空白和尾部空白String s5 = ' An dro id ';String s6 = s5.trim();System.out.println('**********'+s5+'**********');System.out.println('**********'+s6+'**********');//7.比較字符串的內容是否相同System.out.println(s1.equals(s5));//8.與equals方法類似,這個忽略大小寫String s7='abcDef';String s8='ABCDef';System.out.println(s7.equals(s8));//falseSystem.out.println(s7.equalsIgnoreCase(s8));//true//9.將指定字符串連接到此字符串的結尾,等價于'+'String s9='abc';String s10 = s9.concat('def');System.out.println(s10);//10.比較兩個字符串的大小String s11='abe';System.out.println(s9.compareTo(s11)); //-2 說明s9小于s11//11.返回一個新的字符串,它是此字符串的從bedinIndex開始截取到最后的一個子字符串String s12 = '我一定要學會Android';System.out.println(s12.substring(6));//12.返回一個新字符串,它是此字符串從beginIndex開始截取到endIndex(不包括)的一個子字符串String s13 = s12.substring(2, 6);System.out.println(s13); }
輸出結果如下:
當然String中,不止這些方法,只不過這些是比較常用的方法。下面再說一些其他的方法:還是以代碼為例:
@Test public void test2(){//1.測試此字符串是否以指定的后綴結束String s1 = 'helloworld';System.out.println(s1.endsWith('ld'));//true//2.測試此字符串是否以指定的前綴結束System.out.println(s1.startsWith('hel'));//true//3.測試此字符串從指定索引開始的字符串是否以指定前綴開始System.out.println(s1.startsWith('ow', 4));//true//4.當且僅當此字符串包含指定的char值序列時,返回true;System.out.println(s1.contains('lo'));//trueSystem.out.println(s1.contains('lowld'));//false//5.返回指定子字符串在此字符串中第一次出現處的索引System.out.println(s1.indexOf('el'));//1//6.返回指定子字符串在此字符串中第一次出現處的索引,從指定的索引開始System.out.println(s1.indexOf('ow',3));//4//7.返回指定子字符串在此字符串中最右邊出現處的索引System.out.println(s1.lastIndexOf('o'));//6//8.返回指定子字符串在此字符串中最后一次出現處的索引,從指定的索引開始反向搜索System.out.println(s1.lastIndexOf('o', 5));//4 }
下面是String中關于正則的方法:
@Test public void test3(){//1.返回一個新的字符串,它是通過用newChar替換此字符串中出現的所有oldChar得到的String s1='你好,我是程序員小白,小白!';System.out.println(s1.replace(’小’,’大’));//2.使用指定的字面值替換序列,替換此字符串所有匹配字面值目標序列的子字符串System.out.println(s1.replace('小白','大牛'));//3.使用給定的replacement替換此字符串所有匹配給定的正則表達式的子字符串String s2='12Hello2342World234Android';String s3 = s2.replaceAll('d+', ',').replaceAll('^,|,$', '');System.out.println(s3);//4.告知此字符串是否匹配給定的正則表達式String s4='123456';//判斷s4字符串中是否全部由數字組成,即1-n個數字組成boolean matches = s4.matches('d+');System.out.println(matches);String tel='0373-12345678';//判斷這是否是河南的一個固定電話boolean matches1 = tel.matches('0373-d{7,8}');System.out.println(matches1);//5.根據給定正則表達式的匹配拆分此字符串String s5='hello|world|android';String[] split = s5.split('|');for (int i = 0; i < split.length; i++) { System.out.println(split[i]);}System.out.println('****************************');//6.根據匹配給定的正則表達式來拆分此字符串,最多不能超過limit個,如果超過了,剩下的都全部放到最后一個元素中String s6='hello.world.android';String[] split1 = s6.split('.');for (int i = 0; i < split1.length; i++) { System.out.println(split1[i]);} }
輸出結果如下:
到此這篇關于淺談Java中String的常用方法的文章就介紹到這了,更多相關String的常用方法內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: