創建字符串
常見的創建字符串的三種方式:
// 方式一
String str = "hello world";
// 方式二
String str2 = new String("hello world");
// 方式三
char[] array = {'a', 'b', 'c'};
String str3 = new String(array);
"hello" 這樣的字符串字面值常量, 類型也是 String.
String 也是引用類型. String str = "Hello"; 這樣的代碼內存布局如下:
?
?下面我來寫一個代碼:
String str1 = "hallo";
String str2 = str1;
str1 = "world";
Systrm.out.println(str2);
很多人是否會認為 str1 的值改變了因此 str2 的值也跟著改變了,事實上 str1 里面的值并非是改變了而是指向了一個新的字符串。所以 str2 里面的值還是 hallo 。
?此時該代碼在內存中的布局是這樣的:
?判斷字符串相等
在整型中我們判斷兩個整形是否相等用的是一下的方法:
int x = 10;
int y = 10;
if(x == y){System.out.println(true);
}
但是在字符串中我們能否也用這個方法呢?
我們用代碼試試:
我們乍一看好像是可以的,但是我們換一個方法試試呢?
答案是錯誤的!
注意:?String 使用 == 比較并不是在比較字符串內容, 而是比較兩個引用是否是指向同一個對象。
Java 中要想比較字符串的內容, 必須采用String類提供的equals方法。
字符、字節與字符串
字符與字符串
將字符數組轉變為字符串
public class Test {public static void main(String[] args) {char[] array = {'a','b','c','d'};String str1 = new String(array);System.out.println(str1);String str2 = new String(array,0,2);System.out.println(str2);}
}
輸出結果:
?在將字符數組轉變為字符串時既可以將整個數組轉變為字符串也可以指定范圍。
字符串轉變為字符數組
public class Test {public static void main(String[] args) {String str = "abcdef";char[] array = str.toCharArray();for (int i = 0; i < array.length; i++) {System.out.println(array[i]);}}
}
輸出結果:
獲取字符串指定位置的字符
public class Test {public static void main(String[] args) {String str = "abcdef";char ch = str.charAt(1);System.out.println(ch);}
}
字節與字符串
將字符串轉變為字節數組
public class Test {public static void main(String[] args) throws UnsupportedEncodingException {String str = "halloworld";byte[] array = str.getBytes();for (int i = 0; i < array.length; i++) {System.out.println(array[i]);}}
}
運行結果:
字符串常見操作
字符串比較
區分大小寫的比較
public class Test {public static void main(String[] args) {String str1 = "abcd";String str2 = "abcd";if(str1.equals(str2)){System.out.println(true);}else{System.out.println(false);}}
}
?不區分大小寫的比較
public class Test {public static void main(String[] args) {String str1 = "abcd";String str2 = "AbCd";if(str1.equalsIgnoreCase(str2)){System.out.println(true);}else{System.out.println(false);}}
}
比較兩個字符串的大小
public class Test {public static void main(String[] args) {String str1 = "abcd";String str2 = "dsjkowjrd";System.out.println(str1.compareTo(str2));System.out.println(str1.compareToIgnoreCase(str2));}
}
字符串的查找
判斷一個子字符串是否存在
public class Test {public static void main(String[] args) {String str1 = "halloworld";System.out.println(str1.contains("world"));}
}
查找指定字符串的位置
public class Test {public static void main(String[] args) {String str = "halloworld";System.out.println(str.indexOf("ow"));//從開始往后查找子字符串的位置System.out.println(str.indexOf("ow",3));//從指定位置開始往后查找子字符串的位置System.out.println(str.lastIndexOf("ow"));//從后往前查找子字符串的位置System.out.println(str.lastIndexOf("ow",7));//由指定位置從后往前查找子字符串的位置}
}
查找到了則返回子字符串的起始位置,沒有查找到則返回 -1。
判斷字符串是否以給定字符串開頭
public class Test {public static void main(String[] args) {String str = "halloworld";System.out.println(str.startsWith("ha"));//從頭開始判斷字符串是否以該子字符串開頭System.out.println(str.startsWith("ll",2));//從指定位置開始判斷字符串是否以該子字符串開頭}
}
判斷字符串是否以給定字符串結尾
public class Test {public static void main(String[] args) {String str = "halloworld";System.out.println(str.endsWith("ld"));}
}
字符串替換
使用一個指定的新的字符串替換掉已有的字符串數據
public class Test {public static void main(String[] args) {String str = "halloworld";String str1 = str.replaceAll("l","ww");//替換所有的指定內容System.out.println(str1);String str2 = str.replaceFirst("l","ww");//替換第一個出現的指定內容System.out.println(str2);}
}
由于字符串是不可變對象, 替換不修改當前字符串, 而是產生一個新的字符串
字符串拆分
可以將一個完整的字符串按照指定的分隔符劃分為若干個子字符串
public class Test {public static void main(String[] args) {String str = "hallo world zhang san";String[] array = str.split(" ");//按照指定分隔符拆分成若干個子字符串for(String s : array){System.out.println(s);}String[] array1 = str.split(" ",2);//按照指定分隔符拆分成2個字符串for(String s : array1){System.out.println(s);}}
}
拆分是特別常用的操作. 一定要重點掌握. 另外有些特殊字符作為分割符可能無法正確切分, 需要加上轉義。
例如拆分IP地址:
public class Test {public static void main(String[] args) {String str = "192.166.1.1";String[] array = str.split("\\.");for(String s : array){System.out.println(s);}}
}
注意事項:
1. 字符"|","*","+"都得加上轉義字符,前面加上"\".
2. 而如果是"",那么就得寫成"\\".
3. 如果一個字符串中有多個分隔符,可以用"|"作為連字符
字符串的截取
public class Test {public static void main(String[] args) {String str = "halloworld";String str1 = str.substring(5);//從指定位置截取到最后String str2 = str.substring(0,5);//截取一個范圍的內容System.out.println(str1);System.out.println(str2);}
}
去除字符串左右空格保留中間空格
public class Test {public static void main(String[] args) {String str = " hallo world ";String str1 = str.trim();System.out.println(str1);}
}
字符串轉大寫
public class Test {public static void main(String[] args) {String str = "halloworld";String str1 = str.toUpperCase();System.out.println(str1);}
}
字符串轉小寫
public class Test {public static void main(String[] args) {String str = "HALLOworld";String str1 = str.toLowerCase();System.out.println(str1);}
}
字符串入池
public class Test {public static void main(String[] args) {String str = new String("halloworld").intern();}
}
字符串連接
public class Test {public static void main(String[] args) {String str = "hallo";String str1 = str.concat("world");System.out.println(str1);}
}
判斷字符串是否為空
public class Test {public static void main(String[] args) {String str = "";System.out.println(str.isEmpty());}
}
空的意思是該字符串長度為0.