JAVA?String常用方法超詳細(xì)講解
一 、常見String類的獲取功能
(1) length:獲取字符串長(zhǎng)度;
String test ="abcdefg";
System.out.println("長(zhǎng)度為:"+ test.length() );
長(zhǎng)度為: 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碼中對(duì)應(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ì)對(duì)照ASCII 碼表 從第一個(gè)字母進(jìn)行減法運(yùn)算 返回的就是這個(gè)減法的結(jié)果,如果前面幾個(gè)字 母一樣會(huì)根據(jù)兩個(gè)字符串的長(zhǎng)度進(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+子串長(zhǎng)度)為起始的剩下的字符串中子串出現(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA中關(guān)于Long類型返回前端精度丟失問題處理辦法
這篇文章主要介紹了后端JavaBean的id屬性從Long類型改為雪花算法后出現(xiàn)的精度丟失問題,解決方案包括將id字段類型改為字符串或使用Jackson序列化方式,需要的朋友可以參考下2024-11-11
SpringBoot中application.properties與application.yml區(qū)別小結(jié)
本文主要介紹了SpringBoot中application.properties與application.yml區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10
解決IDEA中多模塊下Mybatis逆向工程不生成相應(yīng)文件的情況
這篇文章主要介紹了解決IDEA中多模塊下Mybatis逆向工程不生成相應(yīng)文件的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
mybatis-plus自帶保存接口,主鍵Id不是從1開始問題及解決
文章描述了在使用MyBatis-Plus的save方法保存數(shù)據(jù)時(shí),遇到主鍵ID過大導(dǎo)致報(bào)錯(cuò)的問題,提出的解決方案包括使用@TableId注解讓MyBatis-Plus使用數(shù)據(jù)庫(kù)的自增模式,使用truncatetable方法重置表并備份數(shù)據(jù),以及通過建表語(yǔ)句設(shè)置主鍵ID的初始值2025-12-12
微服務(wù)架構(gòu)之使用RabbitMQ進(jìn)行異步處理方式
本文介紹了RabbitMQ的基本概念、異步調(diào)用處理邏輯、RabbitMQ的基本使用方法以及在Spring Boot項(xiàng)目中使用RabbitMQ解決高并發(fā)問題,RabbitMQ是一種流行的消息隊(duì)列實(shí)現(xiàn),支持異步通信,可以有效解耦應(yīng)用程序的不同部分,并將任務(wù)分發(fā)給多個(gè)消費(fèi)者2025-02-02
springBoot集成Elasticsearch 報(bào)錯(cuò) Health check failed的解決
這篇文章主要介紹了springBoot集成Elasticsearch 報(bào)錯(cuò) Health check failed的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
spring @retryable不生效的一種場(chǎng)景分析
項(xiàng)目中某個(gè)位置要調(diào)用其它部門的接口,一直有問題,對(duì)方讓加重試,這篇文章主要介紹了spring @retryable不生效的一種場(chǎng)景分析,感興趣的朋友跟隨小編一起看看吧2024-07-07
IDEA POM文件配置profile實(shí)現(xiàn)不同環(huán)境切換的方法步驟
這篇文章主要介紹了IDEA POM文件配置profile實(shí)現(xiàn)不同環(huán)境切換的方法步驟2024-03-03

