淺談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));//false System.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"));//true System.out.println(s1.contains("lowld"));//false //5.返回指定子字符串在此字符串中第一次出現(xiàn)處的索引 System.out.println(s1.indexOf("el"));//1 //6.返回指定子字符串在此字符串中第一次出現(xiàn)處的索引,從指定的索引開始 System.out.println(s1.indexOf("ow",3));//4 //7.返回指定子字符串在此字符串中最右邊出現(xiàn)處的索引 System.out.println(s1.lastIndexOf("o"));//6 //8.返回指定子字符串在此字符串中最后一次出現(xiàn)處的索引,從指定的索引開始反向搜索 System.out.println(s1.lastIndexOf("o", 5));//4 }
下面是String中關于正則的方法:
@Test public void test3(){ //1.返回一個新的字符串,它是通過用newChar替換此字符串中出現(xiàn)的所有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字符串中是否全部由數(shù)字組成,即1-n個數(shù)字組成 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.根據(jù)給定正則表達式的匹配拆分此字符串 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.根據(jù)匹配給定的正則表達式來拆分此字符串,最多不能超過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的常用方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
dockerfile-maven-plugin極簡教程(推薦)
這篇文章主要介紹了dockerfile-maven-plugin極簡教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10淺談靜態(tài)變量、成員變量、局部變量三者的區(qū)別
下面小編就為大家?guī)硪黄獪\談靜態(tài)變量、成員變量、局部變量三者的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09阿里SpringBoot應用自動化部署實現(xiàn)IDEA版Jenkins
這篇文章主要為大家介紹了阿里SpringBoot應用自動化部署實現(xiàn)IDEA版Jenkins過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07詳解Spring Boot 項目啟動時執(zhí)行特定方法
這篇文章主要介紹了詳解Spring Boot 項目啟動時執(zhí)行特定方法,Springboot給我們提供了兩種“開機啟動”某些方法的方式:ApplicationRunner和CommandLineRunner。感興趣的小伙伴們可以參考一下2018-06-06SpringMVC 跨重定向請求傳遞數(shù)據(jù)的方法實現(xiàn)
這篇文章主要介紹了SpringMVC 跨重定向請求傳遞數(shù)據(jù)的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06SpringCloud?Tencent?全套解決方案源碼分析
Spring Cloud Tencent實現(xiàn)Spring Cloud標準微服務SPI,開發(fā)者可以基于Spring Cloud Tencent開發(fā)Spring Cloud微服務架構應用,Spring Cloud Tencent 的核心依托騰訊開源的一站式服務發(fā)現(xiàn)與治理平臺 Polarismesh,實現(xiàn)各種分布式微服務場景,感興趣的朋友一起看看吧2022-07-07