標籤:ignore 對象 測試 序列 官方 cas prefix nbsp rar
String代表字串,在Java中,所有的字串字面值都作為此類的執行個體實現
字串的特點以及簡單的原理分析:
package demo;/* * String類的特點: * 所有的""都是String的對象 * 字串一旦建立就是常量,不能改變 */public class StringDemo { public static void main(String[] args) { //發現建立對象不需要new String str = "abcd"; System.out.println(str); //輸出:abcd而不是記憶體位址,因為String類重寫了toString方法 str = "efgh"; System.out.println(str); //輸出:efgh,之前不是說常量不能改變嗎?這裡的原因: //字串本身是一個對象,在堆記憶體中,字串本質是一個字元的數組 //源碼:private final char value[];有final修飾,因此String是常量 //str = "efgh"是在記憶體中重新開一片,str的指向改變了,而"abcd"字串對象沒有改變 }}
String類的建立方式和比較:
package demo;public class StringDemo { public static void main(String[] args) { //字串定義的兩種方式,直接=,使用String的構造方法 String str1 = new String("abc"); String str2 = "abc"; //直接=更為方便,但是存在區別 System.out.println(str1==str2);//false System.out.println(str1.equals(str2));//true //使用構造方法定義原理: //實際上建立了兩個對象,一個是new String對象,一個是"abc"對象 //而new String對象中儲存了"abc"這個字元數組對象的地址 //str1指向new String對象 //列印str1的時候,調用重寫的toString方法找到字串 //由於"abc"對象已經存在 //所以str2直接指向"abc"對象,不建立新的對象 //因此,str1==str2為false,因為對象的地址不同 //而str1.equals(str2)是true,原理: //String類繼承Object,重寫了父類方法equals,建立了字串自己的字串比較方式 //即只要字串的每個字元相同就是true,與地址無關 }}
String類的構造方法:
1.官方資料:String(byte[] byte) 通過使用平台的預設字元集解碼指定的byte數組,構造一個新的String
樣本:
package demo;/* * String類的構造方法 */public class StringDemo2 { public static void main(String[] args) { byte[] bytes = {97,98,99,100}; String s = new String(bytes); System.out.println(s); //輸出:abcd //將位元組數組中的每個位元組,查詢編碼錶得到的結果 }}
2.官方資料:String(byte[] byte, int offset, int length)
樣本:
package demo;/* * String類的構造方法 */public class StringDemo2 { public static void main(String[] args) { byte[] bytes = {65,66,67,68,69}; String s = new String(bytes, 2 ,3); //第二個參數是起始索引,第三個參數是選取個數 System.out.println(s); //輸出:CDE }}
3.官方資料:String (char[] value) 分配一個新的String,使其表示字元數組參數中當前包含的字元序列
樣本:
package demo;/* * String類的構造方法 */public class StringDemo2 { public static void main(String[] args) { function(); } public static void function(){ char[] ch = {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘}; String s = new String(ch); System.out.println(s); }}//輸出:abcde
4.和前兩個相似,官方資料:String(char[] value, int offset, int length)
樣本:
package demo;/* * String類的構造方法 */public class StringDemo2 { public static void main(String[] args) { function(); } public static void function(){ char[] ch = {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘}; String s = new String(ch,1,3); System.out.println(s); }}//輸出:bcd
還有很多的構造方法,不過常用的是這些
String類的其他方法:
1.官方資料:int length() 返回此字串的長度(比較簡單,不做樣本了)
2.官方資料:String substring(int beginIndex) 返回一個新的字串,它是此字串的一個子字串
另一個:String substring(int beginIndex,int endIndex) 同上,多了一個結束索引值
樣本:
package demo;public class StringDemo2 { public static void main(String[] args) { function(); } public static void function(){ String str = "iamhandsome"; String str1 = str.substring(3); String str2 = str.substring(3, 5); System.out.println(str1); System.out.println(str2); }}/*輸出:handsomeha這裡注意,區間是左閉右開,意思是包括起始索引,不包括終止索引*/
3.官方資料:boolean startsWith(String prefix)
測試字串是否以指定的首碼開始
有首碼也應該有尾碼:
boolean endWith(String suffix)
4.官方資料:boolean contains(String s)判斷一個字串中是否包含另一個字串
5.int indexOf(char ch) 尋找一個字元在字串中第一次出現的索引
3,4,5樣本:
package demo;public class StringDemo2 { public static void main(String[] args) { function(); } public static void function() { String string = "Iamhandsome"; boolean b = string.startsWith("Ia"); System.out.println(b);// true boolean b1 = string.endsWith("ome"); System.out.println(b1);// true boolean b2 = string.contains("hand"); System.out.println(b2);//true int a = string.indexOf("a"); System.out.println(a);//1 }}
6.將字串轉成位元組數組:
byte[] getBytes()
此功能和String的構造方法相反
樣本:
package demo;public class StringDemo2 { public static void main(String[] args) { function(); } public static void function() { String str = "abc"; byte[] bytes = str.getBytes(); for(int i=0; i<bytes.length; i++){ System.out.println(bytes[i]); } }}/*輸出:979899*/
7.將字串轉成字元數組:char[] toCharArray();
package demo;public class StringDemo2 { public static void main(String[] args) { function(); } public static void function() { String str = "abc"; char[] chars = str.toCharArray(); for(int i=0; i<chars.length; i++){ System.out.println(chars[i]); } }}/*輸出:abc*/
比較字串是否相同
樣本:
package demo;public class StringDemo2 { public static void main(String[] args) { function(); } public static void function() { String str1 = "Abc"; String str2 = "abc"; boolean b1=str1.equals(str2); boolean b2 = str1.equalsIgnoreCase(str2); System.out.println(b1);//false System.out.println(b2);//true //比較字串是否相同,第二個忽略大小寫 }}
Java學習筆記19(String類)