String類---常用類
阿新 • • 發佈:2020-08-07
java.lang.String類代表字串
程式中所有的雙引號字串,都是string類的物件(就算沒有new,也照樣是)
字串的特點:
1.字串的內容永不改變【重點】
2.正式字串內容永不改變,所以字串是可以共享使用的
3.字串效果上相當於char[]字串陣列,但是底層原理是byte[]位元組陣列
建立字串的常用3+1種方法
三種構造方法:
public String():建立一個空白字串,不含有任何資訊
public String(char[] array):通過一個字元陣列,來建立字串
public String(byte[] array):通過一個位元組陣列來建立字串(本質)
一種直接建立:
String s = "hello";
注意:直接寫上雙引號,就是字串物件。jvm會自動幫你建立
package commonclass; public class TestString01 { public static void main(String[] args) { //空參構造建立字串 String s1 = new String(); System.out.println(s1); //運用字元陣列,建立字串 char[] chars = {'a','b','c','d'}; String s2 = new String(chars); System.out.println(s2); //根據位元組陣列,建立字串 byte[] bytes = {97,98,99}; String s3 = new String(bytes); System.out.println(s3); //直接""建立字串 String s4="abcd"; System.out.println(s4); } }
字串常量池:程式當中直接寫上的雙引號字串,就在字串常量池中。
對於基本型別來說,==是進行數值的比較
對於引用型別來說,==是進行【地址值】的比較
package commonclass; public class TestString02 { public static void main(String[] args) { String str1 = "abc"; String str2 = "abc"; byte[] bytes = {97,98,99}; String str3 = new String(bytes); char[] chars = {'a','b','c'}; String str4 = new String(chars); //對於基本型別來說,==是進行數值的比較 //對於引用型別來說,==是進行【地址值】的比較 System.out.println(str1 == str2);//true System.out.println(str2 == str3);//false System.out.println(str3 == str4);//false System.out.println(str2 == str4);//false } }
string類常用方法
1.對字串內容的比較:
1.1
public boolean equals(Object anObject)//對字串內容的比較,而==是對地址值的比較
注意:
1.equals方法具有對稱性,a.equals(b)和b.equals(a)效果是一樣的
2.如果比較雙方一個常量,一個變數,推薦將常量寫在前面,變數寫在後面
推薦: "abc".equlas(str) 不推薦:str.equlas("abc")
原因:
String str5 = null;
System.out.println("abc".equals(str5));//false
System.out.println(str5.equals("abc"));//會出現NullPointerException
1.2
public boolean equalsIgnoreCase(String anotherString)//忽略大小寫,對內容字串進行比較
2.字串獲取的相關方法
public int length()//返回字串中字元的長度(個數)
public String concat(String str)//將字串拼接到原字串後面,並返回
public char charAt(int index)//返回字串中指定索引值的字元
public int indexOf(String str)//返回引數中的字串在原字串中第一次出現的索引值,-1沒找到
public int indexOf(String str, int fromIndex)//第fromIndex次出現的索引值,-1沒找到
package commonclass;
public class TestString03 {
public static void main(String[] args) {
String str1 = "sdosnksmklmsklmcsdlkmcdskld";
System.out.println("字串的長度:"+str1.length());
System.out.println("abc".concat("de"));
String str2 = "abcdefghijl";
System.out.println(str2.charAt(2));
String str3 = "dhdh000dhdh";
String str4 ="dh";
System.out.println(str3.indexOf(str4));
System.out.println(str3.indexOf(str4,3));//第三次出現的索引值
}
}
3.字串的擷取方法
public String substring(int beginIndex)//擷取返回從beginIndex到末尾索引的字串
public String substring(int beginIndex, int endIndex)//擷取從索引beginIndex,到endIndex索引的字串,注意:[beginIndex,endIndex),左邊索引字元包含,右邊字元不包含
4.字串的轉化相關方法
public char[] toCharArray()//將字串變為字元陣列,並返回
public byte[] getBytes()//將字串變為位元組陣列,並返回
public String replace(CharSequence target, CharSequence replacement)//將原字串中的一部分字串替換成新的字串,並返回。CharSequence,可以接受字串型別
5.字串的分割(將一個字串分割成兩個字串)
public String[] split(String regex) //按照切割規則,將一個字串分割成字串陣列
public String[] split(String regex, int limit) //分割成limit部分(返回的字串陣列的長度)("aa,bb,cc,dd"如果limit是2,結果為"aa"和"bb,cc,dd")
/*
*注意:regex是一個正則表示式,如果按"."進行切分,需要轉義"\\."
*/
String str4 = "aaa.bbb.ccc.ddd";
String[] split1 = str4.split("\\.");//如果是String[] split1 = str4.split("."),split1的長度為0
for (String s : split1) {
System.out.println(s);
}
練習:
package commonclass;
import java.lang.reflect.Array;
//將一個數組{1,2,3}拼接成字串[word1#word2#word3]
/*思路
* 方案一:(不可行,string本身值不變)
* 1.先建立一個字串”[“
* 2.依次讀取陣列中的資料,拼接成Word1#,與原字串拼接str=str+string(最後一個數據須特別對待)
* 3.再拼接字串”]"
* 方案二:
* 先將陣列變為字串,再拼接*/
public class PracticeString01 {
public static void myConcat1(int[] arr){
String str1 = "[";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length-1){
str1 = str1 + "word" + arr[i] + "]";
}else {
str1 = str1 + "word" + arr[i] +"#";
}
}
System.out.println(str1);
}
public static void main(String[] args) {
int[] arr = {1,2,3};
myConcat1(arr);
}
}
package commonclass;
import java.util.Scanner;
//題目:鍵盤輸入一個字串,統計字元的型別個數
//字元型別可分為大寫字母,小寫字母,數字,其他
/*思路:
* 1.讀取控制檯輸入的字串
* 2.將字串變為字元陣列,依次遍歷字元,判斷字元型別
* 3,。輸出不同型別的個數*/
public class PracticeString02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入字串:");
String str = scanner.next();
char[] chars = str.toCharArray();
int M = 0;
int m = 0;
int n =0;
int other = 0;
for (char c : chars) {
if('A' <=c && c <= 'Z') {
M++;
}else if ('a' <= c && c<= 'z'){
m++;
}else if ('0' <= c && c <= '9' ){
n++;
}else {
other++;
}
}
System.out.println("大寫字母有"+M+"個。");
System.out.println("小寫字母有"+m+"個。");
System.out.println("數字有"+n+"個。");
System.out.println("其他字元有"+other+"個。");
}
}