Java中String類的常見方法超詳細講解
一. String類的概念
在Java中 String類是用于表示和操作字符串的類,字符串是Java中常用的數(shù)據(jù)類型,用于存儲文本信息,并且String類中提供了許多方法用來執(zhí)各種字符串操作,那么我們隨著下文來學(xué)習(xí)一下常見的方法。
1.1 String類的特性
1. 不可變性:
String 對象是不可變的,一旦創(chuàng)建了 String 對象,其里面的內(nèi)容就不能被修改,對 String的任何操作實際上都是創(chuàng)建了一個新的 String 對象。
注意:不能修改指的是:String對象的內(nèi)容不能修改,而指向String對象的引用是可以修改的
2. 常量池的概念
字符串常量池是Java中用于存儲字符串字面量的內(nèi)存區(qū)域,為了節(jié)省內(nèi)存,Java使用字符常量池來存儲字符串字面量,當(dāng)創(chuàng)建相同的字符串字面量時,它們會引用常量池中的同一個對象,從而降低程序內(nèi)存的開銷。
二. 字符串的構(gòu)造方式
1. 使用字符串字面量構(gòu)造
public class Test {
public static void main(String[] args) {
String s1 = "hello world"; //直接使用字符串字面量進行構(gòu)造
System.out.println(s1);
}
}2. 通過new關(guān)鍵字創(chuàng)建一個String對象
public class Test {
public static void main(String[] args) {
String s2 = new String("hello world");//通過new關(guān)鍵字創(chuàng)建對象
System.out.println(s2);
}
}3.使用字符數(shù)組進行構(gòu)造
public class Test {
public static void main(String[] args) {
char[] str = {'h','e','l','l','o','w','o','r','l','d'};
String s3 = new String(str); //使用字符數(shù)組進行構(gòu)造
System.out.println(s3);
}
}
以上就是最常見的最常見的字符串構(gòu)造方式。
三. 常用方法
3.1 字符串查找

(1)char charAt( int index)
public class Test {
public static void main(String[] args) {
String str="hello world";
char ch=str.charAt(1);
System.out.println(ch);//e
}
}如果輸入一個不合法的下標(biāo)位置,就會發(fā)生報錯:

(2)int indexOf( int ch)
public class Test {
public static void main(String[] args) {
String str="hello world";
int n=str.indexOf('l');
System.out.println(n); //2
}
}
(3)int indexOf(int ch, int fromIndex)
public class Test {
public static void main(String[] args) {
String str="hello world";
int n=str.indexOf('o',6);
//從下標(biāo)為6的位置(w)開始往后查找o的位置,7
System.out.println(n);
}
}
(4)int indexOf(String str)
public class Test {
public static void main(String[] args) {
String str="hello worldwor";
int n=str.indexOf("wor");
// 返回wor第一次出現(xiàn)的位置,沒有返回-1,所以返回值為6
System.out.println(n);
}
}
(5)int indexOf(String str, int fromIndex)
public class Test {
public static void main(String[] args) {
String str="hello worldwor";
int n=str.indexOf("wor",8);
//從下標(biāo)為8的位置開始找wor第一次出現(xiàn)的位置,沒有返回-1,所以這里返回值為11
System.out.println(n);
}
}
(6)int lastIndexOf(int ch)
public class Test {
public static void main(String[] args) {
String str="hello worldwor";
int n=str.lastIndexOf('w');
//從后往前找,返回w第一次出現(xiàn)的位置,沒有返回-1,所以返回值為11
System.out.println(n);
}
}
(7)int lastIndexOf(int ch, int fromIndex)
public class Test {
public static void main(String[] args) {
String str="hello worldwor";
int n=str.lastIndexOf('w',10);
//從下標(biāo)10的位置開始找,從后往前找w第一次出現(xiàn)的位置,沒有返回-1,所以返回值為6
System.out.println(n);
}
}
(8)int lastIndexOf(String str)
public class Test {
public static void main(String[] args) {
String str="hello worldwor";
int n=str.lastIndexOf("wor");
//從后往前找,返回wor第一次出現(xiàn)的位置,沒有返回-1,返回值為11
System.out.println(n);
}
}
(9)int lastIndexOf(String str, int fromIndex)
public class Test {
public static void main(String[] args) {
String str="hello worldwor";
int n=str.lastIndexOf("wor",10);
//從下標(biāo)為10的位置開始找,從后往前找wor第一次出現(xiàn)的位置,沒有則返回-1,這里返回值為6
System.out.println(n);
}
}
3.2 字符串轉(zhuǎn)換
(1) 數(shù)值和字符串轉(zhuǎn)化
數(shù)值轉(zhuǎn)換成字符串,用String.valueOf( )進行轉(zhuǎn)換:
public class Test {
public static void main(String[] args) {
// 數(shù)字轉(zhuǎn)字符串
String s1 = String.valueOf(123456);
String s2 = String.valueOf(13.14521);
System.out.println(s1);
System.out.println(s2);
}
}數(shù)字字符串轉(zhuǎn)換成數(shù)字,用 Interger.valueOf( )進行轉(zhuǎn)換:
public class Test {
public static void main(String[] args) {
// 字符串轉(zhuǎn)數(shù)字
int n= Integer.valueOf("1234");
double m= Double.valueOf("12.13");
System.out.println(n);
System.out.println(m);
}
}
并且我們也可以傳數(shù)字字符串,所需要轉(zhuǎn)換的進制數(shù):
public class Test {
public static void main(String[] args) {
// 字符串轉(zhuǎn)數(shù)字
int n=Integer.valueOf("1111",2);
//后面的2表示,前面的數(shù)字字符串中的數(shù)字是2進制的形式,我們轉(zhuǎn)換成10進制形式
//就是15
System.out.println(n);
}
}
(2)字母的大小寫轉(zhuǎn)換
字母大小寫轉(zhuǎn)換我們使用String.toUpperCase----小寫轉(zhuǎn)大寫 和 String.toLowerCase----大寫轉(zhuǎn)小寫
public class Test {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
// 小寫轉(zhuǎn)大寫
System.out.println(s1.toUpperCase());//HELLO
// 大寫轉(zhuǎn)小寫
System.out.println(s2.toLowerCase());//hello
}
}
(3)字符串轉(zhuǎn)數(shù)組
我們還可以將一個字符串轉(zhuǎn)換成一個數(shù)組,使用String.toCharArray( )方法進行轉(zhuǎn)換:
public class Test {
public static void main(String[] args) {
String s = "hello";
// 字符串轉(zhuǎn)數(shù)組
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]);
}
}
}
(4)格式化字符串
我們可以使用String.format( )方法進行字符串的格式化:
public class Test {
public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2024, 10,30);
System.out.println(s);
//打印2024-10-30
}
}
3.3 字符串比較
當(dāng)我們比較基本數(shù)據(jù)類型的對象的時候,使用 == 可以進行比較,但是對于我們的String類型(引用類型)來說,使用 == 進行比較,比較的是引用中的地址:

從上圖就可以發(fā)現(xiàn) str1和str2中存放的的字符串是一樣的,但是運行結(jié)果顯示并不相等,那么當(dāng)我們需要進行字符串比較的時候該怎么辦呢?
3.3.1 equals( )方法
equals( )方法的比較方式:按照字典序比較--->比較的是字符大小的順序

3.3.2 compare To( )方法
compare To( )方法的比較方法:按照字典序進行比較 ----> compare To( )方法與equals( )方法的區(qū)別:equals( )方法的返回值是 boolean 類型的值,而 compare To( )方法返回的是 int 類型的值。
1. 先按照字典次序大小比較,如果出現(xiàn)不等的字符,直接返回這兩個字符的大小差值。
2. 如果前k個字符相等(k為兩個字符長度最小值),返回值兩個字符串長度差值。

3.3.3 compare ToIgnoreCase( )方法
該方法與compare To( )方法沒有很大差別,但是compare ToIgnoreCase( )方法在進行比較的時候能夠忽略字符的大小寫:

3.4 字符串替換
3.4.1 replace( )方法
replace( )方法能夠?qū)⒁粋€字符串中指定的字符替換成一個新指定的字符:

3.4.2 replaceFrist( )方法
replaceFrist( )方法能夠?qū)⒆址兄付ǖ牡谝粋€字符串替換成新指定的字符串:

3.4.3 replaceAll( )方法
replaceAll( )方法 與 replaceFrist( )方法不同的是: replaceAll( )方法能將字符串中所有指定的字符串全部替換成一個新指定的字符串:

3.5 字符串拆分
3.5.1 String[] split(String regex)
String[] split( )方法能夠?qū)⒁粋€字符串按照指定的字符為分割線,分割成若干個子字符串:

3.5.2 String[] split(String regex, int limit)
String[] split(String regex, int limit)方法能夠?qū)⒆址凑罩付ǖ母袷?,分割?strong> limit 個子字符串:

3.6 字符串截取
3.6.1 String substring(int beginIndex)
將字符串從下標(biāo)為 beginIndex 的位置開始截取到字符串結(jié)尾:

3.6.2 String substring(int beginIndex, int endIndex)
將字符串從下標(biāo)為 beginIndex 的位置開始截取到 endIndex 位置的前一個字符為止:

四.StringBuilder和StringBuffer
在開頭的String類的特征中,我們就說明了String類的不可變性,對 String 對象的任何修改操作都會創(chuàng)建一個新的對象,在程序運行的過程中,效率是很低的,那么這個時候如果要對字符串進行修改盡量使用StringBuffer或者StringBuilder。
由于 String 的不可更改特性,為了方便字符串的修改, Java 中又提供 StringBuilder 和 StringBuffer 類。這兩個類大部分功能是相同的
在這里介紹一下Stringbuilder一些常用的方法。
4.1 append(String str)
append(String str)方法可以在字符串的末尾追加新的內(nèi)容:

注意:追加的不只是字符串,還可以追加:boolean、char、char[]、double、float、int、long、Object、String、StringBuff類型的變量。
4.2 setCharAt(int index, char ch)
setChatAt(int index, char ch)方法能夠?qū)?index位置的字符替換成ch:

4.3 reverse( )
將字符串進行反轉(zhuǎn):

4.4 capacity( )
capacity( ) 方法可以獲取底層保存字符串空間總的大小:

4.5 ensureCapacity(int mininmumCapacity)
將該字符串的內(nèi)存空間擴展到指定的空間大?。?/p>

4.6 insert(int offset, String str)
在指定的下標(biāo)位置插入一個字符串:

4.7 deleteCharAt(int index)
刪除指定下標(biāo)的字符:

補充:delete(int start, int end)能夠刪除[start, end)區(qū)間內(nèi)的字符:

4.8 replace(int start, int end, String str)
將[start, end)位置的字符替換為str:

注意:String和StringBuilder類不能直接轉(zhuǎn)換。如果要想互相轉(zhuǎn)換,可以采用如下原則
1. String變?yōu)镾tringBuilder: 利用StringBuilder的構(gòu)造方法或append()方法
2. StringBuilder變?yōu)镾tring: 調(diào)用toString()方法
總結(jié)
到此這篇關(guān)于Java中String類的常見方法的文章就介紹到這了,更多相關(guān)Java String類常見方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringSecurity的TokenStore四種實現(xiàn)方式小結(jié)
本文主要介紹了SpringSecurity的TokenStore四種實現(xiàn)方式小結(jié),分別是InMemoryTokenStore,JdbcTokenStore,JwkTokenStore,RedisTokenStore,具有一定的參考價值,感興趣的可以了解一下2024-01-01
基于java SSM springboot實現(xiàn)抗疫物質(zhì)信息管理系統(tǒng)
這篇文章主要介紹了基于JAVA SSM springboot實現(xiàn)的抗疫物質(zhì)信息管理系統(tǒng),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
Springboot+netty實現(xiàn)Web聊天室
這篇文章主要介紹了利用springboot+netty實現(xiàn)一個簡單Web聊天室,文中有非常詳細的代碼示例,對正在學(xué)習(xí)Java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-12-12

