JAVA?String常用方法超詳細(xì)講解
一 、常見String類的獲取功能
(1) length:獲取字符串長度;
String test ="abcdefg"; System.out.println("長度為:"+ test.length() );
長度為: 7
(2) charAt(int index):獲取指定索引位置的字符;
String test ="abcdefg"; System.out.println( "索引為0既第一個(gè)字符為:" + test.charAt(0) );
索引為0既第一個(gè)字符為: a
(3) indexOf(int ch):返回指定字符在此字符串中第一次出現(xiàn)處的索引;(數(shù)字是ASCII碼中對應(yīng)的字符數(shù)值)
String test ="abcdefg"; System.out.println( test.indexOf(97) ); System.out.println( test.indexOf("b") );
0
1
(4) substring(int start):從指定位置開始截取字符串,默認(rèn)到末尾;
String test ="abcdefg"; System.out.println( test.substring(2) );
cdefg
(5) substring(int start,int end):從指定位置開始到指定位置結(jié)束截取字符串;
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): 判斷字符串中是否包含傳遞進(jìn)來的字符串;
String test ="abcdefg"; System.out.println( test.contains("ab") );
true
(3)startsWith(String str): 判斷字符串是否以傳遞進(jìn)來的字符串開頭;
String test ="abcdefg"; System.out.println( test.startsWith("ab") );
true
(4)endsWith(String str): 判斷字符串是否以傳遞進(jìn)來的字符串結(jié)尾;
String test ="abcdefg"; System.out.println( test.endsWith(fg));
true
(5)isEmpty(): 判斷字符串的內(nèi)容是否為空串"";
String test ="abcdefg"; System.out.println( test.isEmpty());
false
三、常見String類的轉(zhuǎn)換功能
(1)byte[] getBytes(): 把字符串轉(zhuǎn)換為字節(jié)數(shù)組;
String test ="abcdefg"; byte[] test1 =test.getBytes(); System.out.println( test1[0] );
97
(2)char[] toCharArray(): 把字符串轉(zhuǎn)換為字符數(shù)組;
String test ="abcdefg"; char[] test1=test.toCharArray(); System.out.println( test1[0] );
a
(3)String valueOf(char[] chs): 把字符數(shù)組轉(zhuǎn)成字符串。valueOf可以將任意類型轉(zhuǎn)為字符串;
char[] test ={'a','b','c','d'}; String test1=String.valueOf(test); System.out.println(test1);
abcd
(4)toLowerCase(): 把字符串轉(zhuǎn)成小寫;
toUpperCase(): 把字符串轉(zhuǎn)成大寫;
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) 將指定字符進(jìn)行互換
String test ="abcdefg"; System.out.println(test.replace("a","A"));
Abcdefg
(2)replace(String old,String new) 將指定字符串進(jìn)行互換
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)
會(huì)對照ASCII 碼表 從第一個(gè)字母進(jìn)行減法運(yùn)算 返回的就是這個(gè)減法的結(jié)果,如果前面幾個(gè)字 母一樣會(huì)根據(jù)兩個(gè)字符串的長度進(jìn)行減法運(yùn)算返回的就是這個(gè)減法的結(jié)果,如果連個(gè)字符串一摸一樣 返回的就是0。
String test ="abcdefg"; String test1 ="abcdefg"; String test2 ="abcdefgh"; System.out.println(test.compareTo(test1) ); System.out.println(test.compareTo(test2) );
0
-1
字符串練習(xí):一個(gè)子串在字符串中出現(xiàn)的次數(shù)
思路:
1.用indexOf方法獲取子串在字符串中第一次出現(xiàn)的位置index
2.再用indexOf方法獲取以(index+子串長度)為起始的剩下的字符串中子串出現(xiàn)的位置,直到字符串中不再包含子串??捎脀hile循環(huán)實(shí)現(xiàn)。
3.每次找到后用計(jì)數(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; } }
總結(jié)
到此這篇關(guān)于JAVA String常用方法的文章就介紹到這了,更多相關(guān)JAVA String常用方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用Spring boot的@Transactional進(jìn)行事務(wù)管理
這篇文章介紹了SpringBoot中使用@Transactional注解進(jìn)行聲明式事務(wù)管理的詳細(xì)信息,包括基本用法、核心配置參數(shù)、關(guān)鍵注意事項(xiàng)、調(diào)試技巧、最佳實(shí)踐以及完整示例,感興趣的朋友一起看看吧2025-02-02mybatis逆向工程與分頁在springboot中的應(yīng)用及遇到坑
最近在項(xiàng)目中應(yīng)用到springboot與mybatis,在進(jìn)行整合過程中遇到一些坑,在此將其整理出來,分享到腳本之家平臺(tái)供大家參考下2018-09-09如何使用Spring Security實(shí)現(xiàn)用戶-角色-資源的權(quán)限控制
文章介紹了如何通過SpringSecurity實(shí)現(xiàn)用戶-角色-資源的權(quán)限管理,包括基于角色的請求控制、加載用戶角色信息、角色與資源的關(guān)聯(lián)等步驟,同時(shí),提供了一些測試場景,以驗(yàn)證權(quán)限控制是否正確,感興趣的朋友跟隨小編一起看看吧2024-10-10spring boot實(shí)現(xiàn)驗(yàn)證碼功能
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。這篇文章主要介紹了spring boot實(shí)現(xiàn)驗(yàn)證碼功能,需要的朋友可以參考下2018-04-04HttpClient 在Java項(xiàng)目中的使用詳解
HttpClient作為訪問Http服務(wù)的客戶端訪問程序已經(jīng)被廣泛使用,提高了開發(fā)效率,也提高了代碼的健壯性。因此熟練掌握HttpClient是必需的,關(guān)于httpclient感興趣的朋友可以參考本篇文章2015-10-10