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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot中application.properties與application.yml區(qū)別小結
本文主要介紹了SpringBoot中application.properties與application.yml區(qū)別小結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-10-10
解決IDEA中多模塊下Mybatis逆向工程不生成相應文件的情況
這篇文章主要介紹了解決IDEA中多模塊下Mybatis逆向工程不生成相應文件的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
mybatis-plus自帶保存接口,主鍵Id不是從1開始問題及解決
文章描述了在使用MyBatis-Plus的save方法保存數(shù)據(jù)時,遇到主鍵ID過大導致報錯的問題,提出的解決方案包括使用@TableId注解讓MyBatis-Plus使用數(shù)據(jù)庫的自增模式,使用truncatetable方法重置表并備份數(shù)據(jù),以及通過建表語句設置主鍵ID的初始值2025-12-12
springBoot集成Elasticsearch 報錯 Health check failed的解決
這篇文章主要介紹了springBoot集成Elasticsearch 報錯 Health check failed的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
IDEA POM文件配置profile實現(xiàn)不同環(huán)境切換的方法步驟
這篇文章主要介紹了IDEA POM文件配置profile實現(xiàn)不同環(huán)境切換的方法步驟2024-03-03

