Java之String類常用操作方法舉例
一、String類的理解
1、類的聲明
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
final:String是不可以被繼承的;
Serializable:可序列化的接口,凡是實(shí)現(xiàn)此接口的類的對象就可以通過網(wǎng)絡(luò)或者本地流進(jìn)行數(shù)據(jù)的傳輸。
Comparable:凡是實(shí)現(xiàn)此接口的類,其對象都可以比較大小。
2、內(nèi)部聲明的屬性
private final char value[];
存儲字符串?dāng)?shù)據(jù)的容器
final:指明此value數(shù)組一旦初始化,其地址就不可變
3、字符串常量的存儲位置
字符串常量都存儲在字符串常量池(StringTable)中
字符串常量池不允許存放兩個(gè)相同的字符串常量
字符串常量池在不同的jdk版本中,存放的位置不同
4、字符串的不可變性的理解
1、當(dāng)對字符串變量重新賦值時(shí),需要重新指定一個(gè)字符串常量的位置進(jìn)行賦值,不能在原來的位置修改
2、對現(xiàn)有的字符串進(jìn)行拼接操作時(shí),需要重新開辟空間保存新的字符串。
3、當(dāng)調(diào)用字符串的replace方法替換現(xiàn)有的某個(gè)字符時(shí),需要重新開辟空間保存修改以后的字符串,不能原地修改
public class StringDemo { public static void main(String[] args) { StringDemo s = new StringDemo(); s.test2(); s.test3(); } // todo String的不可變性 // 當(dāng)對字符串變量重新賦值時(shí),需要重新指定一個(gè)字符串常量的位置進(jìn)行賦值,不能在原來的位置修改 // 對現(xiàn)有的字符串進(jìn)行拼接操作時(shí),需要重新開辟空間保存新的字符串。 // 當(dāng)調(diào)用字符串的replace方法替換現(xiàn)有的某個(gè)字符時(shí),需要重新開辟空間保存修改以后的字符串,不能原地修改 public void test2() { String s1 = "hello"; String s2 = "hello"; s2 = "hi"; s2+="world"; System.out.println(s1); // todo hello System.out.println(s1); // todo hello } public void test3() { String s1 = "hello"; String s2 = "hello"; String s3=s2.replace('l','o'); System.out.println(s1); // hello System.out.println(s2); // hello System.out.println(s3); // heooo } }
5、String實(shí)例化的兩種方式
String s1 = “hello”;
String s2 = new String(“hello”);
public class StringDemo1 { public static void main(String[] args) { StringDemo1 s = new StringDemo1(); s.test1(); } public void test1(){ String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); String s4 = new String("hello"); System.out.println(s1==s2); //true System.out.println(s1==s3); //false System.out.println(s1==s4); //false System.out.println(s3==s4); //false System.out.println(s1.equals(s2)); //true System.out.println(s1.equals(s3)); //true System.out.println(s1.equals(s4)); //true System.out.println(s3.equals(s2)); //true } }
6、字符串的拼接
1、常量+常量:結(jié)果仍然存儲在字符串常量池;此時(shí)的常量可能是字面量,也可能是final修飾的變量。
2、常量+變量 或者 變量+常量:都會通過new的方式創(chuàng)建一個(gè)新的字符串,返回堆空間中此字符串對象的地址
3、調(diào)用字符串的intern():返回字面量的地址
public void test2() { String s1 = "hello"; String s2 = "world"; String s3 = "helloworld"; String s4 = "hello" + "world"; String s5 = s1 + "world"; //todo 通過查看字節(jié)碼文件發(fā)現(xiàn)調(diào)用了StringBuilder()——》new String() String s6 = "hello" + s2; String s7 = s1 + s2; System.out.println("------------------------------"); System.out.println(s3 == s4); //true System.out.println(s3 == s5); //false System.out.println(s3 == s6); //false System.out.println(s3 == s7); //false System.out.println(s5 == s6); //false System.out.println(s5 == s7); //false }
public void test3() { final String s1 = "hello"; final String s2 = "world"; String s3 = "helloworld"; String s4 = "hello" + "world"; String s5 = s1 + "world"; //todo 通過查看字節(jié)碼文件發(fā)現(xiàn)調(diào)用了StringBuilder()——》new String() String s6 = "hello" + s2; String s7 = s1 + s2; System.out.println("------------------------------"); System.out.println(s3 == s5); //true System.out.println(s3 == s6); //true }
二、String的構(gòu)造器
1、構(gòu)造器
public String() :初始化新創(chuàng)建的 String對象,以使其表示空字符序列。
public String(String original):初始化一個(gè)新創(chuàng)建的“String”對象,使其表示一個(gè)與參教相同的字符序列
public String(char[] value):通過當(dāng)前參數(shù)中的字符數(shù)組來構(gòu)造新的String。
public String(char[] valve,int offset,int count):通過字符數(shù)組的一部分來構(gòu)造新的String。
public String(byte[] bytes):通過使用平臺的默認(rèn)字符集解碼當(dāng)前參數(shù)中的字節(jié)數(shù)組來構(gòu)造新的String。
public String(byte[] bytes,String charsetName)':通過使用指定的字符集解碼當(dāng)前參數(shù)中的字節(jié)數(shù)組來構(gòu)造新的String。
2、String和char之間相互轉(zhuǎn)換
String——》char[]:調(diào)用String的toCharArray()方法
char——》String:調(diào)用String的構(gòu)造器
public class StringMethodTest { public static void main(String[] args) { StringMethodTest s = new StringMethodTest(); s.test1(); s.test2(); s.test3(); } public void test1() { String s1 = new String(); String s2 = new String(""); String s3 = new String(new char[]{'a', 'b', 'c'}); System.out.println(s3); } public void test2() { String str = "hello"; //todo String——》char[]:調(diào)用String的toCharArray()方法 char[] arr = str.toCharArray(); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } //todo char——》String:調(diào)用String的構(gòu)造器 String str1 = new String(arr); System.out.println(str1); //hello } }
3、String和byte之間相互轉(zhuǎn)換
String——》byte[]:調(diào)用String的getBytes()方法
getBytes(String charsetName):使用指定的字符集
在utf-8字符集中,一個(gè)漢字占用3個(gè)字節(jié),一個(gè)字母占用1個(gè)字節(jié)。
在gbk字符集中,一個(gè)漢字占用2個(gè)字節(jié),一個(gè)字母占用1個(gè)字節(jié)。
public class StringMethodTest { public static void main(String[] args) { StringMethodTest s = new StringMethodTest(); s.test1(); s.test2(); s.test3(); } // String與byte[]之間的轉(zhuǎn)換 // 在utf-8字符集中,一個(gè)漢字占用3個(gè)字節(jié),一個(gè)字母占用1個(gè)字節(jié)。 // 在gbk字符集中,一個(gè)漢字占用2個(gè)字節(jié),一個(gè)字母占用1個(gè)字節(jié)。 public void test3() { String str = "中國"; //todo String——》byte[]:調(diào)用String的toCharArray()方法 byte[] arr = str.getBytes(); //使用默認(rèn)的字符集 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); // -28\-72\-83\-27\-101\-67 } System.out.println(); // String str1 = new String("abc中國"); // // todo getBytes(String charsetName):使用指定的字符集 // byte[] arr1 = str1.getBytes("gbk"); //使用默認(rèn)的字符集 // for(int i = 0;i<arr.length;i++){ // System.out.println(arr[i]); // 101\101\108\108\111 // } // byte[]——》String String str2 = new String(arr); System.out.println(str2); //中國 } }
三、String中常用方法
1、boolean isEmpty():字符串是否為空;
2、int length():返回字符串的長度;
3、String concat(xx):字符串拼接;
4、boolean equals(Object obj):比較字符串是否相等,區(qū)分大小寫;
5、boolean equalsIgnoreCase(Object obj):比較字符串是否相等,不區(qū)分大小寫;
6、int compareTo(String other):比較字符串大小,區(qū)分大小寫,按照Unicode編碼值比較大小;
7、int compareTolgnoreCase(String other):比較字符串大小,不區(qū)分大小寫;
8、String toLowerCase():將字符串中大寫字母轉(zhuǎn)為小寫;
9、String toUpperCase():將字符串中小寫字母轉(zhuǎn)為大寫;
10、String trim():去掉字符串前后空白符;
11、public String intern():結(jié)果在常量池中共享;
12、boolean contains(xx):是否包含xx
13、int indexOf(xx):從前往后找當(dāng)前字符串中xx,即如果有返回第一次出現(xiàn)的下標(biāo),要是沒有返回-1;
14、int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出現(xiàn)處的索引開始向后找;
15、int lastIndexOf(xx):從后往前找當(dāng)前字符串中xx,即如果有返回最后一次出現(xiàn)的下標(biāo),要是沒有返回-1
16、int lastIndexOf(String str,int fromIndex):返回指定子字符串在此字符串中最后一次出現(xiàn)處的并且向前找;
17、String substring(int beginIndex):返回一個(gè)新的字符串,它是此字符串的從beginIndex開始截取;
18、String substring(int beginIndex,int endIndex):返回一個(gè)新字符串,它是此字符串從beginIndex開始截取,到endIndex結(jié)束;
19、char charAt(index):返回[index]位置的字符
20、char[] toCharArray(): 將此字符串轉(zhuǎn)換為一個(gè)新的字符數(shù)組返回
21、static String valueOf(char[] data):返回char數(shù)組參數(shù)的字符串表示形式
22、static String valueOf(char[] data,int offset,int count): 返回char數(shù)組參數(shù)的特定子數(shù)組的字符串表示形式
23、static String copyValueOf(char[] data): 返回指定數(shù)組中表示該字符序列的字符串。
24、static String copyValue0f(char[] data,int offset,int count):返回指定數(shù)組中指定片段的字符串。start:開始下標(biāo) count:長度
25、boolean startsWith(xx):方法用于檢測字符串是否以指定的子字符串開始。
26、boolean startsWith(string prefix,int toffset):如果字符串以指定的前綴開始,則返回 true;否則返回 false。
27、boolean endsWith(xx):測試此字符串是否以指定的后結(jié)束
28、String replace(char oldchar,char newchar):返回一個(gè)新的字符串,它是通過用 newchar 替換oldchar;
29、String replace(CharSequence target,charSequence replacement):用replacement替換所有的target,兩個(gè)參數(shù)都是字符串。
30、String replaceAll(String regex,String replacement):用replacement替換所有的regex匹配項(xiàng),regex很明顯是個(gè)正則表達(dá)式,replacement是字符串。
31、String replaceFirst(String regex,String replacement):基本和replaceAll相同,區(qū)別是只替換第一個(gè)匹配項(xiàng)。
總結(jié)
到此這篇關(guān)于Java之String類常用操作方法的文章就介紹到這了,更多相關(guān)Java String類操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于spring AOP @Around @Before @After的區(qū)別說明
這篇文章主要介紹了基于spring AOP @Around @Before @After的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Springboot實(shí)例講解實(shí)現(xiàn)專業(yè)材料認(rèn)證管理系統(tǒng)流程
這是一個(gè)基于java的畢業(yè)設(shè)計(jì)項(xiàng)目,畢設(shè)課題為springboot框架的知識產(chǎn)權(quán)服務(wù)平臺系統(tǒng),是一個(gè)采用b/s結(jié)構(gòu)的javaweb項(xiàng)目,需要的朋友可以參考下2022-06-06Maven中optional和scope元素的使用弄明白了嗎
這篇文章主要介紹了Maven中optional和scope元素的使用弄明白了嗎,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Java中的線程池ThreadPoolExecutor細(xì)致講解
這篇文章主要介紹了Java中的線程池ThreadPoolExecutor細(xì)致講解,線程池是一種基于池化思想管理線程的工具,經(jīng)常出現(xiàn)在多線程服務(wù)器中,如MySQL,線程過多會帶來額外的開銷,其中包括創(chuàng)建銷毀線程的開銷、調(diào)度線程的開銷等等,需要的朋友可以參考下2023-11-11前端如何傳遞Array、Map類型數(shù)據(jù)到Java后端
這篇文章主要給大家介紹了關(guān)于前端如何傳遞Array、Map類型數(shù)據(jù)到Java后端的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01解讀@ResponseBody與@RequestBody注解的用法
這篇文章主要介紹了Spring MVC中的@ResponseBody和@RequestBody注解的用法,@ResponseBody注解用于將Controller方法的返回對象轉(zhuǎn)換為指定格式(如JSON)并通過Response響應(yīng)給客戶端,@RequestBody注解用于讀取HTTP請求的內(nèi)容2024-11-11