java中String的一些方法深入解析
1、public String(char[] c,begin,length).
從字符數(shù)組c的下標(biāo)begin處開始,將長度為length的字符數(shù)組轉(zhuǎn)換為字符串。
begin與length可以省略,即將字符數(shù)組c轉(zhuǎn)換為字符串。另:字符數(shù)組可改為字節(jié)數(shù)組byte[] b.
char[] c=new char[]{'j','y','6','a','4','t','9'};
String s1=new String(c);
String s=new String(c,2,3);
System.out.println(s1);
System.out.println(s);
2、public char[] toCharArray().
字符串裝換成字符數(shù)組。

3、public char charAt(int 下標(biāo)).
返回字符串中指定位置的字符。
String s="jkdfsdf";
char t=s.charAt(3);
4、public byte[] getBytes().
將一個字符串轉(zhuǎn)換成字節(jié)數(shù)組,其默認(rèn)輸出為ASCII值,可通過char強(qiáng)制類型轉(zhuǎn)換輸出字節(jié)。String s="sjdfsdf";
byte[] b=s.getBytes();
5、public String trim().
清除字符串左右兩端的空格。
String s="skkgnsdfsd ";
System.out.println(s.trim());
6、public int indexOf(String s,int index).
從字符串中查找指定位置之后指定的字符所在的位置。若不指定位置,則從頭開始。
String s="dgdgdg";
int n=s.indexOf("t");//從頭開始查找
int n1=s.indexOf("d",3);//從位置3處開始查找
7、public String substring(int beginindex,int endindex ).
截取所指定的從開始位置到結(jié)束位置的字符串,不包含結(jié)束字符。結(jié)束位置可以省略。
String s="sdgsgghd";
String s1=s.substring(2,4);
String s2=s.substring(2);
8、public String[] split(String s).
通過指定的字符分割字符串。
String s="dfgdhdfgdrhrhgdt";
String ss[]=s.split("d");
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);
9、public String toUpperCase()./public String toLowerCase().字符大小寫轉(zhuǎn)換。
String s="dfgdhdfgdrhrhgdt";
String s1=s.toUpperCase();//字符全大寫
String s2=s.toLowerCase();//字符全小寫
10、public boolean startsWith(String s)./public boolean endsWith(String s).檢測字符串是否是以指定的字符開始/結(jié)尾。
String s="dfdhffghrtgfjn mjg";
boolean t1=s.startsWith("e");
boolean t2=s.endsWith("h");
11、判斷字符串是否相等,區(qū)分大小寫:equals()。不區(qū)分大小寫equalsIgnoreCase().
String s="dfgdghdf";
String s1="sfsgsdu";
s.equals(s1);
12、public String replaceAll(String s,String s1).將字符串中的s都替換成s1.
String s="dfgdghdf";
String s1=s.replaceAll("d","f");
相關(guān)文章
Spring?Boot整合?NoSQL?數(shù)據(jù)庫?Redis詳解
這篇文章主要為大家介紹了Spring?Boot整合?NoSQL?數(shù)據(jù)庫?Redis詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Java中finally和return的關(guān)系實例解析
這篇文章主要介紹了Java中finally和return的關(guān)系實例解析,總結(jié)了二者的關(guān)系,然后分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
SpringBoot+Logback實現(xiàn)一個簡單的鏈路追蹤功能
Spring Boot默認(rèn)使用LogBack日志系統(tǒng),并且已經(jīng)引入了相關(guān)的jar包,所以我們無需任何配置便可以使用LogBack打印日志。這篇文章主要介紹了SpringBoot+Logback實現(xiàn)一個簡單的鏈路追蹤功能,需要的朋友可以參考下2019-10-10
springboot /tmp 臨時目錄的具體實現(xiàn)
springboot應(yīng)用服務(wù)再啟動的時候,會在操作系統(tǒng)的/tmp目錄,本文主要介紹了springboot /tmp 臨時目錄的具體實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-06-06

