常用類(3):String類
一、String類概述
1、String類的特點介紹
觀察API發現:
1、String代表的是字符串,屬於java.lang下面的,所以使用的時候不需要導包
2、String類代表字符串
Java程序中的所有字符串文字(例如"abc" )都被實現為此類的實例(實例代表的是對象)
3、字符串不變; 它們的值在創建后不能被更改
(意思是字符串是常量,一旦被賦值,就不能改變)
2、字符串含義
就是由多個字符組成的數據,叫做字符串;也可以看作是一個字符數組
二、String類的構造方法
(1)public String()
//重寫了Object類中的toString方法
(2)public String(byte[] bytes)
//將字節數組轉成一個字符串
(3)public String(byte[] bytes,int offset,int length)
//將字節數組中的一部分截取出來變成一個字符串
(4)public String(char[] value)
//將字符數組轉成一個字符串
(5)public String(char[] value,int offset,int count)
//將字符數組中的一部分截取出來變成一個字符串
(6)public String(String original)
(1)public String()
public class StringDemo {
public static void main(String[] args) {
//使用構造方法創建對象
String s = new String();
System.out.println(s);
}
}
//打印的結果是空的(什么也沒有),說明重寫了Object類中的toString方法
查看字符串的長度: 對象名.length()
public class StringDemo {
public static void main(String[] args) {
//使用構造方法創建對象
String s = new String();
//查看字符串的長度
//API:public int length()返回此字符串的長度
System.out.println("字符串的長度為:"+s.length());
}
}
執行結果如下:
字符串的長度為:0
Process finished with exit code 0
(2) public String(byte[] bytes)
public String(byte[] bytes)
意思是:構造方法String需要傳入一個字節數組,將這個字節數組轉成一個字符串
public class StringDemo {
public static void main(String[] args) {
//public String(byte[] bytes) 將一個字節數組轉成一個字符串
//首先創建一個byte類型的數組
byte[] b = {97,98,99,100,};
//將b傳入對象中
String s = new String(b);
System.out.println("數組轉換成字符串為:"+s);
System.out.println("字符串的長度為:"+s.length());
}
}
執行結果如下:
數組轉換成字符串為:abcd
字符串的長度為:4
Process finished with exit code 0
(3)public String(byte[] bytes,int offset,int length)
public String(byte[] bytes,int offset,int length)
意思是:構造方法String傳入一個字節數組,將這個字節數組中的一部分截取出來變成一個字符串
(offset理解為:下標、索引)
public class StringDemo {
public static void main(String[] args) {
//首先創建一個byte類型的數組
byte[] b = {97,98,99,100,101,102};
String s = new String(b);
System.out.println("轉換為的字符串:"+s);//abcdef
//創建對象,將b傳入進去
String s1 = new String(b,1,3);//從下標(索引)為1開始截,截3個
System.out.println("截取后的字符串為:"+s1);
}
}
執行結果如下:
轉換為的字符串:abcdef
截取后的字符串為:bcd
Process finished with exit code 0
//截取的時候注意不要超出邊界
(4)public String(char[] value)
public String(char[] value)
構造方法String需要傳入一個字符數組,將這個字符數組轉成一個字符串
public class StringDemo {
public static void main(String[] args) {
//創建一個字符數組
char[] c = {'a','b','c','d','我','愛','阿','偉'};
//創建對象,將數組c傳入進去
String s = new String(c);
System.out.println("字符轉換為字符串為:"+s);
}
}
執行結果如下:
字符轉換為字符串為:abcd我愛阿偉
Process finished with exit code 0
(5)public String(char[] value,int offset,int count)
public String(char[] value,int offset,int count)
構造方法String傳入一個字符數組,將這個字符數組中的一部分截取出來變成一個字符串
public class StringDemo {
public static void main(String[] args) {
//創建一個字符數組
char[] c = {'a','b','c','d','我','愛','阿','偉'};
//創建對象,將數組c傳入進去
String s = new String(c);
System.out.println("字符轉換為字符串為:"+s);
String s1 = new String(c, 4, 4);//從索引為4開始截,截4個
System.out.println("截取后的字符串為:"+s1);
}
}
執行結果如下:
字符轉換為字符串為:abcd我愛阿偉
截取后的字符串為:我愛阿偉
Process finished with exit code 0
(6)public String(String original)
/*
1、方法區中有一個常量池區域,Java虛擬機現在常量池中找“你好”,如果有將“你好”地址值賦給s;
如果沒有,在常量池里開辟一個空間,空間里有“你好”,地址值假設為0x001,然后將0x001賦給s
然后String s這個空間指向“你好”這個常量池
2、String s1 = new String(s);表示在棧里面開辟一個區域叫String s1
new String(s)表示將在堆里面開辟一個區域叫new String(s),假設地址值為0x0001;
new String(s)這個區域里面放的是“你好”字符串的地址值0x001,
然后 new String(s)這個區域指向“你好”常量池,
new String(s)這個區域的地址值賦給s1,s1指向這個區域
3、自始至終,內存里就一個“你好”字符串
以上分析為說明API里的一句話:
初始化新創建的String對象,使其表示與參數相同的字符序列; 換句話說,新創建的字符串是參數字符串的副本。
除非需要original的顯式副本, original使用此構造函數是不必要的,因為Strings是不可變的
*/
案例:
/*
字符串是常量,它的值在創建之后不能更改
String s = “hello”;
s += “world”;
問s的結果是多少?
*/
public class StringDemo2 {
public static void main(String[] args) {
String s = "hello";
s += "world";
System.out.println(s);
}
}
執行結果如下:
helloworld
Process finished with exit code 0
為Strings是不可變的,那為什么程序沒報錯,反而執行出來了?
1、Java虛擬機在常量池中沒有找到“hello”,那么會開辟一個空間,"hello"放進去,假設地址值為0x01,
將0x01賦給s;s通過地址值0x01指向“hello”
2、Java虛擬機在常量池中沒有找到“world”,那么也會開辟一個空間,“world”放進去,假設地址值為0x02
然后拿着“hello”進行拼接,得到一個新的字符串“helloworld”,假設“helloworld”地址值為0x02,
將0x02賦給s,s指向“helloworld”
特點:字符串一旦被創建,它的值就不能被改變,指的是字符串在常量池中的值不能改變
4、String類的面試題
/*
String s = new String(“hello”)和String s = “hello”;的區別?
字符串比較之看程序寫結果
字符串拼接之看程序寫結果
*/
public class StringDemo3 {
public static void main(String[] args) {
//創建對象,將hello傳入進去
String s1 = new String("hello");
String s2 = "hello";
System.out.println(s1==s2);
System.out.println(s1.equals(s2)); //true
}
}
執行結果如下:
false
true
Process finished with exit code 0
結果分析:
1、==比較引用數據類型的時候,比較的是地址值
String s1 = new String("hello");在堆內存中創建對象
String s1指向堆,String s2指向常量池
所以二者結果不一樣,即false
2、String類中重寫了Object的equals方法,equals方法默認比較的是地址值,但是由於重寫 了,所以s1與s2比較的是內容hello,即true
5、例題
public class StringDemo5 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = "hello"+"world";
System.out.println(s3==s4); //true
System.out.println(s3==s1+s2); //false
System.out.println(s3.equals(s1+s2)); //true
執行結果為:
true
false
true
Process finished with exit code 0
結果分析:
字符串如果是常量相加,是先相加,然后去常量池中找,如果找到了,就返回,如果找不到就創建;
字符串如果是變量相加,是先開辟空間,然后再拼接,拼接后獲得了一個全新的地址值
s4是"hello"、"world"兩個常量相加,s3==s1+s2表示s3是兩個變量相加
即s3==s4是true,s3==s1+s2是false
如果想明確獲取地址值,可以使用API工具類中System類中的identityHashCode()方法來獲取
public class StringDemo5 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = "hello"+"world";
String s5 = s1+s2;
System.out.println(System.identityHashCode(s1));//356573597
System.out.println(System.identityHashCode(s2));//1735600054
System.out.println(System.identityHashCode(s3));//21685669
System.out.println(System.identityHashCode(s4));//21685669
System.out.println(System.identityHashCode(s5));//2133927002
}
}
三、String類的判斷功能
(1)boolean equals(Object obj)
//比較字符串的內容是否相同 區分大小寫
(2)boolean equalsIgnoreCase(String str)
//比較字符串的內容是否相同,忽略大小寫
(3)boolean contains(String str)
//判斷大的字符串中是否包含小的字符串,如果包含,返回true,反之false,區分大小寫
(4)boolean startsWith(String str)
//測試此字符串是否以指定的前綴開頭,區分大小寫
(5)boolean endsWith(String str)
//測試此字符串是否以指定的后綴結束,區分大小寫
(6)boolean isEmpty()
//判斷字符串是否是空字符串
(1)boolean equals(Object obj):比較字符串的內容是否相同 區分大小寫
public class StringDemo6 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
String s4 = "";
String s5 = null;//null沒有地址值
System.out.println(s1.equals(s2));//false
System.out.println(s1.equals(s3));//false
System.out.println(s4.equals(s5));//false
}
}
(2)boolean equalsIgnoreCase(String str):比較字符串的內容是否相同,忽略大小寫
public class StringDemo6 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
System.out.println(s1.equalsIgnoreCase(s2));//true
System.out.println(s1.equalsIgnoreCase(s3));//true
}
}
(3)boolean contains(String str):
判斷大的字符串中是否包含小的字符串,如果包含,返回true,反之false,區分大小寫
public class StringDemo6 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
//當且僅當此字符串包含指定的char值序列時才返回true。
//判斷大的字符串中是否包含小的字符串,如果包含,返回true,反之false,區分大小寫
System.out.println(s1.contains("Hello"));//false
System.out.println(s1.contains("leo"));//false
System.out.println(s1.contains("hello"));//true
}
}
(4)boolean startsWith(String str):測試此字符串是否以指定的前綴開頭,區分大小寫
public class StringDemo6 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
//測試此字符串是否以指定的前綴開頭,區分大小寫
System.out.println(s1.startsWith("hel"));//true
System.out.println(s1.startsWith("h"));//true
System.out.println(s1.startsWith("he"));//true
System.out.println(s1.startsWith("he34"));//false
System.out.println(s1.startsWith("H"));//false
}
}
(5)boolean endsWith(String str):測試此字符串是否以指定的后綴結束,區分大小寫
public class StringDemo6 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
//測試此字符串是否以指定的后綴結束,區分大小寫
System.out.println(s1.endsWith("orld"));//true
System.out.println(s1.endsWith("orlD"));//false
}
}
(6)boolean isEmpty():判斷字符串是否是空字符串
public class StringDemo6 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
String s4 = "";
String s5 = null;
String s6 = "bigdata";
String s7 = null;
//boolean isEmpty() 判斷字符串是否是空字符串
System.out.println(s1.isEmpty());//false
System.out.println(s4.isEmpty());//true
System.out.println(s6.equals(s7));//false
//null不能調方法,會報錯
//System.out.println(s5.isEmpty());
//System.out.println(s7.equals(s6));
}
}
注意:
字符串之間比較的要求,在不知道兩個字符串變量的值的時候,為了防止空指針異常,把變量放在后面
案例:
//需求:將s6,s7與"hadoop"進行比較
System.out.println("hadoop".equals(s6));
System.out.println("hadoop".equals(s7));
四、String類的獲取功能
(1)int length()
//獲取字符串的長度
(2)char charAt(int index)
//返回char字符指定位置索引的值,索引從0開始到length()-1
(3)int indexOf(int ch)
//返回指定字符第一次出現的字符串內的索引,如果此字符串中沒有此類字符,則返回-1
(4)int indexOf(String str)
//返回的是字符串中第一個字符在大字符串中的索引值,如果值不存在,則返回-1
(5)int indexOf(int ch,int fromIndex)
//返回指定字符第一次出現在字符串中的索引,以指定的索引開始搜索,如果值不存在,則返回-1
(6)int indexOf(String str,int fromIndex)
//返回字符串中第一個字符在大字符串中的索引值,以指定的索引開始搜索,如果值不存在,則返回-1
(7)String substring(int start)
//子字符串從指定的索引處開始,並擴展到字符串的末尾,包含開始索引位置的
(8)String substring(int start,int end)
//字串開始於start位置,並截取到end-1的位置,左閉右開(含頭不含尾)
(1)int length():獲取字符串的長度
public class StringDemo7 {
public static void main(String[] args) {
String s = "helloworld";
//int length() 獲取字符串的長度
System.out.println(s.length());//10
}
}
(2)char charAt(int index):返回char字符指定位置索引的值,索引從0開始到length()-1
public class StringDemo7 {
public static void main(String[] args) {
String s = "helloworld";
//char charAt(int index) 返回char字符指定位置索引的值,索引從0開始到length()-1
System.out.println(s.charAt(7));//r
System.out.println(s.charAt(0));//h
}
}
(3)int indexOf(int ch):返回指定字符第一次出現的字符串內的索引,如果此字符串中沒有此類字符,則返回-1
public class StringDemo7 {
public static void main(String[] args) {
String s = "helloworld";
//int indexOf(int ch)
//返回指定字符第一次出現的字符串內的索引,如果此字符串中沒有此類字符,則返回-1
System.out.println(s.indexOf('l'));//2
System.out.println(s.indexOf(97));//-1
}
}
(4)int indexOf(String str):返回的是字符串中第一個字符在大字符串中的索引值,如果值不存在,則返回-1
public class StringDemo7 {
public static void main(String[] args) {
String s = "helloworld";
//int indexOf(String str)
//helloworld
//返回的是字符串中第一個字符在大字符串中的索引值,如果值不存在,則返回-1
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("qwer"));
}
}
(5)int indexOf(int ch,int fromIndex):
返回指定字符第一次出現在字符串中的索引值,以指定的索引開始搜索,如果值不存在,則返回-1
public class StringDemo7 {
public static void main(String[] args) {
String s = "helloworld";
//int indexOf(int ch,int fromIndex)
//返回指定字符第一次出現在字符串中的索引,以指定的索引開始搜索,如果值不存在,則返回-1
System.out.println(s.indexOf('l',4));//從索引4開始搜索,在索引8第一次出現字符'l'
System.out.println(s.indexOf('p',4)); //-1
System.out.println(s.indexOf('l',40));//-1
System.out.println(s.indexOf('p',40));//-1
}
}
(6)int indexOf(String str,int fromIndex):
返回字符串中第一個字符在大字符串中的索引值,以指定的索引開始搜索,如果值不存在,則返回-1
(7)String substring(int start):
子字符串從指定的索引處開始,並擴展到字符串的末尾,,包含開始索引位置的值
public class StringDemo7 {
public static void main(String[] args) {
String s = "helloworld";
//String substring(int start)
//返回的是一個字符串,該字符串是此字符串的子字符串
//子字符串從指定的索引處開始,並擴展到字符串的末尾,包含開始索引位置的值
System.out.println(s.substring(3)); //loworld
}
}
(8)String substring(int start,int end):字串開始於start位置,並截取到end-1的位置,左閉右開(含頭不含尾)
public class StringDemo7 {
public static void main(String[] args) {
String s = "helloworld";
//String substring(int start,int end)
//返回的是一個字符串,該字符串是此字符串的子字符串
//字串開始於start位置,並截取到end-1的位置,左閉右開(含頭不含尾)
System.out.println(s.substring(5,10));//world
}
}
五、String類的轉換功能
字符串的轉換功能:
(1)byte[] getBytes()
//使用平台的默認字符集將此String編碼為字節序列,將結果存儲到新的字節數組中
(2)char[] toCharArray()
//將字符串轉換成字符數組
(3)static String valueOf(char[] chs)
//將字符數組轉換成字符串(需要傳入參數)
(4)static String valueOf(int i)
//將int類型的數據轉換成字符串類型(需要傳入參數)
(5)String toLowerCase()
//將字符串中的內容全部轉換成小寫
(6)String toUpperCase()
//將字符串中的內容全部轉換成大寫
(7)String concat(String str)
//將小括號中的字符串拼接到大字符串的后面(需要傳入參數)
(1)byte[] getBytes():使用平台的默認字符集將此String編碼為字節序列,將結果存儲到新的字節數組中
public class StringDemo {
public static void main(String[] args) {
String s = "HelloWorLD";
//byte[] getBytes()
//使用平台的默認字符集將此String編碼為字節序列,將結果存儲到新的字節數組中
byte[] b = s.getBytes();//s.getBytes().var回車
//數組需要遍歷,如果直接輸出,將會是一個地址值
//System.out.println(b1); //[B@4554617c
for(int i=0;i<b1.length;i++){
System.out.println(b[i]);
}
}
}
執行結果如下:
72
101
108
108
111
87
111
114
76
68
Process finished with exit code 0
(2)char[] toCharArray():將字符串轉換成字符數組
public class StringDemo {
public static void main(String[] args) {
String s = "HelloWorLD";
//char[] toCharArray()
//將字符串轉換成字符數組
char[] c = s.toCharArray();
//第一個方法遍歷
for(int i=0;i<c1.length;i++){
System.out.printIn(c[i]);
}
/*
第二個方法遍歷
增強for循環,后面集合的時候會講解,它是用來替代迭代器的
for(char c : c1){
System.out.print(c);
}
*/
}
}
執行結果如下:
H
e
l
l
o
W
o
r
L
D
Process finished with exit code 0
(3)static String valueOf(char[] chs):將字符數組轉換成字符串(需要傳入參數)
public class StringDemo {
public static void main(String[] args) {
String s = "HelloWorLD";
//先將字符串轉換成字符數組
char[] c = s.toCharArray();
//static String valueOf(char[] chs)
//將字符數組轉換成字符串
//該方法是靜態的,通過類名直接調取
String s = String.valueOf(c);
System.out.println(s);
}
}
執行結果如下:
HelloWorLD
Process finished with exit code 0
(4)static String valueOf(int i):將int類型的數據轉換成字符串類型
public class StringDemo {
public static void main(String[] args) {
//static String valueOf(int i)
//將int類型的數據轉換成字符串類型
String s = String.valueOf(100); //100 -->"100"
System.out.println(s2); //100
}
}
(5)String toLowerCase():將字符串中的內容全部轉換成小寫
public class StringDemo {
public static void main(String[] args) {
String s = "HelloWorLD";
//String toLowerCase()
//將字符串中的內容全部轉換成小寫
String s1 = s.toLowerCase();
System.out.println(s1); //helloworld
}
}
(6)String toUpperCase():將字符串中的內容全部轉換成大寫
public class StringDemo {
public static void main(String[] args) {
String s = "HelloWorLD";
//String toUpperCase()
//將字符串中的內容全部轉換成大寫
String s1 = s.toUpperCase();
System.out.println(s1); //HELLOWORLD
}
}
(7)String concat(String str):將小括號中的字符串拼接到大字符串的后面(需要傳入參數)
public class StringDemo {
public static void main(String[] args) {
String s = "HelloWorLD";
//String concat(String str)
//將小括號中的字符串拼接到大字符串的后面(需要傳入參數)
String s1 = s.concat("hadoop");
System.out.println(s1);//HelloWorLDhadoop
}
}
六、String類的其他功能(替換、去除空格、字典順序比較)
/*
替換功能
String replace(char old,char new)//字符替換字符
String replace(String old,String new)//字符串替換字符串
去除字符串兩空格
String trim()
按字典順序比較兩個字符串
int compareTo(String str)
int compareToIgnoreCase(String str)
*/
public class StringDemo11 {
public static void main(String[] args) {
String s = "helloworldowodadadwowo";
//String replace(char old,char new)
//將新的字符替換字符中指定的所有字符,並返回新的字符串
String s1 = s.replace('l', 'a');//把所有的l用a替換
System.out.println(s1);//heaaoworadowodadadwowo
//String replace(String old,String new)
//將字符串中舊的小串用新的小串替換,返回一個新的字符串
String s2 = s.replace("owo", "wow");
System.out.println(s2);//hellwowrldwowdadadwwow
String s3 = s.replace("owo", "qwerdf");
System.out.println(s3);//hellqwerdfrldqwerdfdadadwqwerdf
//如果被替換的字符串不存在,返回的是原本的字符串
String s4 = s.replace("qwer", "poiu");
System.out.println(s4);//helloworldowodadadwowo
//String trim() 去除字符串兩邊的空格
String s5 = " hello world ";
System.out.println(s5); // hello world
System.out.println(s5.trim()); //hello world
//int compareTo(String str)
String s6 = "hello";//首字母h的ASCII碼值104
String s7 = "hello";//首字母h的ASCII碼值104
String s8 = "abc"; //首字母a的ASCII碼值97
String s9 = "qwe"; //首字母q的ASCII碼值113
System.out.println(s6.compareTo(s7)); //0 104-104=0
System.out.println(s6.compareTo(s8)); //7 104-97=7
System.out.println(s6.compareTo(s9)); //-9 104-113=-9
//如果首字母一樣,字符個數不一樣,個數相減
String s10 = "hel";
System.out.println(s6.compareTo(s10)); //2 5-3=2
}
}