JAVA?String常用方法超詳細講解
一 、常見String類的獲取功能
(1) length:獲取字符串長度;
String test ="abcdefg"; System.out.println("長度為:"+ test.length() );
長度為: 7
(2) charAt(int index):獲取指定索引位置的字符;
String test ="abcdefg"; System.out.println( "索引為0既第一個字符為:" + test.charAt(0) );
索引為0既第一個字符為: a
(3) indexOf(int ch):返回指定字符在此字符串中第一次出現(xiàn)處的索引;(數(shù)字是ASCII碼中對應的字符數(shù)值)
String test ="abcdefg"; System.out.println( test.indexOf(97) ); System.out.println( test.indexOf("b") );
0
1
(4) substring(int start):從指定位置開始截取字符串,默認到末尾;
String test ="abcdefg"; System.out.println( test.substring(2) );
cdefg
(5) substring(int start,int end):從指定位置開始到指定位置結束截取字符串;
String test ="abcdefg"; System.out.println( test.substring(2,5));
cde
二、常見String類的判斷功能
(1)equals(Object obj): 比較字符串的內(nèi)容是否相同,區(qū)分大小寫;
String test ="abcdefg"; String test1 ="abcdefg"; System.out.println( test.equals(test1) );
true
(2)contains(String str): 判斷字符串中是否包含傳遞進來的字符串;
String test ="abcdefg"; System.out.println( test.contains("ab") );
true
(3)startsWith(String str): 判斷字符串是否以傳遞進來的字符串開頭;
String test ="abcdefg"; System.out.println( test.startsWith("ab") );
true
(4)endsWith(String str): 判斷字符串是否以傳遞進來的字符串結尾;
String test ="abcdefg"; System.out.println( test.endsWith(fg));
true
(5)isEmpty(): 判斷字符串的內(nèi)容是否為空串"";
String test ="abcdefg"; System.out.println( test.isEmpty());
false
三、常見String類的轉換功能
(1)byte[] getBytes(): 把字符串轉換為字節(jié)數(shù)組;
String test ="abcdefg"; byte[] test1 =test.getBytes(); System.out.println( test1[0] );
97
(2)char[] toCharArray(): 把字符串轉換為字符數(shù)組;
String test ="abcdefg"; char[] test1=test.toCharArray(); System.out.println( test1[0] );
a
(3)String valueOf(char[] chs): 把字符數(shù)組轉成字符串。valueOf可以將任意類型轉為字符串;
char[] test ={'a','b','c','d'}; String test1=String.valueOf(test); System.out.println(test1);
abcd
(4)toLowerCase(): 把字符串轉成小寫;
toUpperCase(): 把字符串轉成大寫;
String test ="abcdefg"; String test1 ="ABCDEFG"; System.out.println(test1.toLowerCase()); System.out.println(test.toUpperCase());
abcdefg
ABCDEFG
(5)concat(String str): 把字符串拼接;
String test ="abcdefg"; String test1 ="higk"; System.out.println(test.concat(test1));
abcdefghigk
四、常見String類的其他常用功能
(1)replace(char old,char new) 將指定字符進行互換
String test ="abcdefg"; System.out.println(test.replace("a","A"));
Abcdefg
(2)replace(String old,String new) 將指定字符串進行互換
String test ="abcdefg"; System.out.println(test.replace("ab","AB"));
ABcdefg
(3)trim() 去除兩端空格
String test =" abcdefg "; System.out.println(test ); System.out.println(test.trim());
&abcdefg&(代表空格)
abcdefg
(4)int compareTo(String str)
會對照ASCII 碼表 從第一個字母進行減法運算 返回的就是這個減法的結果,如果前面幾個字 母一樣會根據(jù)兩個字符串的長度進行減法運算返回的就是這個減法的結果,如果連個字符串一摸一樣 返回的就是0。
String test ="abcdefg"; String test1 ="abcdefg"; String test2 ="abcdefgh"; System.out.println(test.compareTo(test1) ); System.out.println(test.compareTo(test2) );
0
-1
字符串練習:一個子串在字符串中出現(xiàn)的次數(shù)
思路:
1.用indexOf方法獲取子串在字符串中第一次出現(xiàn)的位置index
2.再用indexOf方法獲取以(index+子串長度)為起始的剩下的字符串中子串出現(xiàn)的位置,直到字符串中不再包含子串??捎脀hile循環(huán)實現(xiàn)。
3.每次找到后用計數(shù)器記錄即可。
public class StringTest_2 { public static void main(String[] args) { String str="abcqwabcedcxabcuabcjkabcnmbabc"; //String str=null; try { int count=countChildStr(str,"abc"); System.out.println("abc在"+str+"中出現(xiàn)的次數(shù)為:"+count); } catch (NullPointerException ne) { System.out.println(ne); } catch(RuntimeException re) { System.out.println(re); } } public static int countChildStr(String str,String key) { if(str==null||key==null) { throw new NullPointerException("空指針異常,源字符串和子串都不能為NULL"); } if(key=="") {throw new RuntimeException("調(diào)用不合法,子串要有內(nèi)容");} int count=0,index=0; while((index=str.indexOf(key,index))!=-1) { count++; index+=key.length(); } return count; } }
總結
到此這篇關于JAVA String常用方法的文章就介紹到這了,更多相關JAVA String常用方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
如何使用Spring boot的@Transactional進行事務管理
這篇文章介紹了SpringBoot中使用@Transactional注解進行聲明式事務管理的詳細信息,包括基本用法、核心配置參數(shù)、關鍵注意事項、調(diào)試技巧、最佳實踐以及完整示例,感興趣的朋友一起看看吧2025-02-02mybatis逆向工程與分頁在springboot中的應用及遇到坑
最近在項目中應用到springboot與mybatis,在進行整合過程中遇到一些坑,在此將其整理出來,分享到腳本之家平臺供大家參考下2018-09-09如何使用Spring Security實現(xiàn)用戶-角色-資源的權限控制
文章介紹了如何通過SpringSecurity實現(xiàn)用戶-角色-資源的權限管理,包括基于角色的請求控制、加載用戶角色信息、角色與資源的關聯(lián)等步驟,同時,提供了一些測試場景,以驗證權限控制是否正確,感興趣的朋友跟隨小編一起看看吧2024-10-10