文章目錄
- String
- 字符串比較
- 字符串查找
- 轉化
- 字符串替換
- 字符串拆分
- 字符串截取(常用)
- 字符串的不可變性

String
- str本來是字符串常量的引用,應該打印地址,但是編譯器重寫了toString方法,所以打印hello
- String 的構造方法
public class test {public static void main(String[] args) {// 用常量字符串構造String s1 = "hello";System.out.println(s1);// 用字符數組構造char[] array = new char[]{'a','b','c'};String s2 = new String(array);System.out.println(s2);char[] array1 = new char[]{'a','b','c'};String s4 = new String(array,0,2);// 從0位置后拿2個字符System.out.println(s4);// 直接newSting對象構造String s3 = new String("hello");System.out.println(s3);}
}
- String對象在內存中的情況
4. 空指針異常和空字符串,isEmpty()判斷是否為空字符串
字符串比較
- s1 == s2 比較地址
- s1.equals(s2)比較是否相等,返回true或者false
- s1.compareTo(s2)比較大小
- s1.compareToIgonreCase(s2)忽略大小寫比較
public class test {public static void main(String[] args) {String s1 = new String("hello");String s2 = new String("Hello");System.out.println(s1 == s2);// 不等于,s1和s2表示對象的引用都存的是地址System.out.println(s1.equals(s2));System.out.println(s1.compareTo(s2));// s1 大于 s2 返回正數// s1 小于 s2 返回負數// s1 等于 s2 返回0System.out.println(s1.compareToIgnoreCase(s2));// 忽略大小寫比較}
}
字符串查找
- char charAt(int index),返回數組中下標對應的字符
public static void main(String[] args) {String s1 = new String("hello");char ch = s1.charAt(1);System.out.println(ch);// e}
- int indexOf(char ch),返回第一次出現ch字符的下標
String s2 = new String("hello");int index = s1.indexOf('l');System.out.println(index);// 2
- int indexOf(char ch,int k),k表示下標,從指定位置開始查找
String s2 = new String("hello");int index = s1.indexOf('l',3);System.out.println(index);// 3
- int indexOf(String s),可以查找子串在主串中出現的位置,如果沒有找到返回-1
String s3 = "ababcdeabcf";int index = s3.indexOf("abc");System.out.println(index);// 2int index1 = s3.indexOf("abc",3);System.out.println(index1);// 7
- int lastIndexOf(String s),倒著往前找,返回第一個找到的下標
String s3 = "ababcabcd";int index = s3.lastIndexOf("abc");System.out.println(index);// 5int index1 = s3.lastIndexOf("abc",4);System.out.println(index1);// 2// 從4下標位置倒著往前找
轉化
- 數字和字符串之間的轉化
String s2 = String.valueOf(new Student("zhangsan",20));System.out.println(s2);String s3 = String.valueOf(123);System.out.println(s3);String s4 = String.valueOf(123.34);System.out.println(s4);String s5 = String.valueOf(true);System.out.println(s5);// true
2. 字符串轉數字
int a = Integer.parseInt("190");System.out.println(a);int b = Integer.parseInt("19.9");// 錯誤,給的要是整數的字符串
- 大小寫轉化
小寫轉大寫:toUpperCase
轉變為大寫不是在原有字符串的基礎上轉換,而是轉變為大寫是一個新的對象,不會改變原有的字符串
String s = "hello";String ret = s.toUpperCase();System.out.println(ret);
大寫轉小寫
String s = "HEllo";
String ret = s.toLowerCase();System.out.println(ret);// hello
- 字符串轉為數組
String s = "hello";char[] ret = s.toCharArray();System.out.println(Arrays.toString(ret));// [h,e,l,l,o]
數組轉為字符串
char[] ret = {'a','b','c'};String s = new String(ret);System.out.println(s);// abc
- 格式轉換
String s = String.format("%d-%d-%d",2019,9,20);
System.out.println(s);// 2019-9-20
字符串替換
- 替換字符串
- 替換單個字符
3. 替換第一個ab
4. 把所有的ab都替換為123
字符串拆分
- s.split()以這里面的字符串為標準分割
public static void main(String[] args) {String s = "hello world k";String[] array = s.split(" ");System.out.println(Arrays.toString(array));// [hello,world,k]String s1 = "hello world k";String[] array1 = s.split(" ",2);// 以空格分割最多分成兩組System.out.println(Arrays.toString(array1));// [hello, world k]}
- 特殊的情況,使用轉義字符
多次分割
字符串截取(常用)
- substring(a,b) [a,b) a和b均為下標
String str = "hello";
String ret = str.substring(0,3);// hel
- 給一個參數,會把后面的全不截取
String str = "hello";
String ret = str.substring(0);// hello
- trim(),去掉字符串的左右空格,中間的空格不可去掉
字符串的不可變性
- 字符串中的value[] 數組是被private修飾的,也沒有提供get方法,在類外是無法拿到的,就無法修改該數組了
- 而被final修飾的,只是表明它是常量了,它的引用只能指向一個對象,不能被改變成指向別的對象
- 被final修飾的不能被繼承
final int[] array = {1,2,3};
// array = new int[]{1,2};
// 不能改變array的指向了
array[0] = 2;// 可被修改