Java之String類常用操作方法舉例
一、String類的理解
1、類的聲明
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
final:String是不可以被繼承的;
Serializable:可序列化的接口,凡是實現(xiàn)此接口的類的對象就可以通過網(wǎng)絡(luò)或者本地流進(jìn)行數(shù)據(jù)的傳輸。
Comparable:凡是實現(xiàn)此接口的類,其對象都可以比較大小。
2、內(nèi)部聲明的屬性
private final char value[];
存儲字符串?dāng)?shù)據(jù)的容器
final:指明此value數(shù)組一旦初始化,其地址就不可變
3、字符串常量的存儲位置
字符串常量都存儲在字符串常量池(StringTable)中
字符串常量池不允許存放兩個相同的字符串常量
字符串常量池在不同的jdk版本中,存放的位置不同
4、字符串的不可變性的理解
1、當(dāng)對字符串變量重新賦值時,需要重新指定一個字符串常量的位置進(jìn)行賦值,不能在原來的位置修改
2、對現(xiàn)有的字符串進(jìn)行拼接操作時,需要重新開辟空間保存新的字符串。
3、當(dāng)調(diào)用字符串的replace方法替換現(xiàn)有的某個字符時,需要重新開辟空間保存修改以后的字符串,不能原地修改
public class StringDemo {
public static void main(String[] args) {
StringDemo s = new StringDemo();
s.test2();
s.test3();
}
// todo String的不可變性
// 當(dāng)對字符串變量重新賦值時,需要重新指定一個字符串常量的位置進(jìn)行賦值,不能在原來的位置修改
// 對現(xiàn)有的字符串進(jìn)行拼接操作時,需要重新開辟空間保存新的字符串。
// 當(dāng)調(diào)用字符串的replace方法替換現(xiàn)有的某個字符時,需要重新開辟空間保存修改以后的字符串,不能原地修改
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實例化的兩種方式
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é)果仍然存儲在字符串常量池;此時的常量可能是字面量,也可能是final修飾的變量。
2、常量+變量 或者 變量+常量:都會通過new的方式創(chuàng)建一個新的字符串,返回堆空間中此字符串對象的地址
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):初始化一個新創(chuàng)建的“String”對象,使其表示一個與參教相同的字符序列
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字符集中,一個漢字占用3個字節(jié),一個字母占用1個字節(jié)。
在gbk字符集中,一個漢字占用2個字節(jié),一個字母占用1個字節(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字符集中,一個漢字占用3個字節(jié),一個字母占用1個字節(jié)。
// 在gbk字符集中,一個漢字占用2個字節(jié),一個字母占用1個字節(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):返回一個新的字符串,它是此字符串的從beginIndex開始截取;
18、String substring(int beginIndex,int endIndex):返回一個新字符串,它是此字符串從beginIndex開始截取,到endIndex結(jié)束;
19、char charAt(index):返回[index]位置的字符
20、char[] toCharArray(): 將此字符串轉(zhuǎn)換為一個新的字符數(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):返回一個新的字符串,它是通過用 newchar 替換oldchar;
29、String replace(CharSequence target,charSequence replacement):用replacement替換所有的target,兩個參數(shù)都是字符串。
30、String replaceAll(String regex,String replacement):用replacement替換所有的regex匹配項,regex很明顯是個正則表達(dá)式,replacement是字符串。
31、String replaceFirst(String regex,String replacement):基本和replaceAll相同,區(qū)別是只替換第一個匹配項。
總結(jié)
到此這篇關(guān)于Java之String類常用操作方法的文章就介紹到這了,更多相關(guān)Java String類操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
約定優(yōu)于配置_動力節(jié)點(diǎn)Java學(xué)院整理
以前做項目,總是寫Ant配置文件,滿足于自己更靈活的配置,而沒有去思考,這么做到底值不值得2017-08-08
淺談Java代理(jdk靜態(tài)代理、動態(tài)代理和cglib動態(tài)代理)
下面小編就為大家?guī)硪黄獪\談Java代理(jdk靜態(tài)代理、動態(tài)代理和cglib動態(tài)代理)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
Java中優(yōu)先隊列PriorityQueue常用方法示例
這篇文章主要介紹了Java中優(yōu)先隊列PriorityQueue常用方法示例,PriorityQueue是一種特殊的隊列,滿足隊列的“隊尾進(jìn)、隊頭出”條件,但是每次插入或刪除元素后,都對隊列進(jìn)行調(diào)整,使得隊列始終構(gòu)成最小堆(或最大堆),需要的朋友可以參考下2023-09-09
Java concurrency之AtomicReference原子類_動力節(jié)點(diǎn)Java學(xué)院整理
AtomicReference是作用是對"對象"進(jìn)行原子操作。這篇文章主要介紹了Java concurrency之AtomicReference原子類,需要的朋友可以參考下2017-06-06
SpringBoot整合Kafka完成生產(chǎn)消費(fèi)的方案
網(wǎng)上找了很多管理kafka整合springboot的教程,但是很多都沒辦法應(yīng)用到生產(chǎn)環(huán)境,很多配置都是缺少,或者不正確的,只能當(dāng)個demo,所以本文給大家介紹了SpringBoot整合Kafka完成生產(chǎn)消費(fèi)的方案,需要的朋友可以參考下2024-12-12
使用resty Quartz執(zhí)行定時任務(wù)的配置方法
這篇文章主要為大家介紹了使用resty?Quartz來執(zhí)行定時任務(wù)的配置方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Java使用wait/notify實現(xiàn)線程間通信上篇
wait()和notify()是直接隸屬于Object類,也就是說所有對象都擁有這一對方法,下面這篇文章主要給大家介紹了關(guān)于使用wait/notify實現(xiàn)線程間通信的相關(guān)資料,需要的朋友可以參考下2022-12-12


