Java 程序中的所有字符串字面值(如 "abc" )都作爲此類的實例實現。
翻譯成人話:程序當中所有雙引號字符串常量,都是String類的對象。(就算沒有new,也照樣是對象。)
字符串的特點:
1. 【字符串的內容不可改變】。(每當你覺得好像是變了的時候,必然是創建了一個新的字符串。)
2. 正是因爲字符串不可改變,所以字符串是可以共享使用的。
3. 字符串就是很多個字符連在一起,效果上看類似於char[],但是底層原理用的是byte[]字節數字進行存儲。
常見的3+1中字符串創建方式:
三種構造方法
public String():無參默認構造方法,產生一個空白字符串。
public String(char[] array):根據字符數組,創建一個對應的字符串。
public String(byte[] array):根據字節數組,創建一個對應的字符串。
一種直接定義
String str = "Hello"
*/
public class Demo01String {
public static void main(String[] args) {
// 默認的無參空白字符串
String str1 = new String();
System.out.println(str1);
// 根據字符數組創建字符串
char[] chars = { 'a', 'b', 'c' };
String str2 = new String(chars);
System.out.println(str2); // abc
// 根據字節數組創建字符串(現階段只考慮英文情況)
byte[] bytes = { 65, 98, 99 };
String str3 = new String(bytes);
System.out.println(str3); // abc
// 直接定義創建,沒有new,也沒有調用構造方法,但是仍然是字符串的對象。
String str4 = "Hello";
System.out.println(str4); // Hello
}
}
// 直接定義創建,沒有new,也沒有調用構造方法,但是仍然是字符串的對象。
String str4 = "Hello";
System.out.println(str4); // Hello
}
}
*
==運算符:
對於基本數據類型來說,進行數據值比較。
對於引用數據類型來說,進行【地址值】比較。
*/
public class Demo02StringPool {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
byte[] bytes = {97, 98, 99};
String str3 = new String(bytes); // abc
System.out.println(str1 == str2); // true
System.out.println(str1 == str3); // false
System.out.println(str2 == str3); // false
}
}
字符串的==運算符,是地址值比較。
如果希望進行內容比較,應該使用方法。
字符串當中與比較相關的方法有:
public boolean equals(Object another):讓當前字符串和另一個參數進行比較,返回內容是否相同。
備註:參數Object類型,意思是雖然什麼參數都行。但是隻有參數是字符串類型的時候,纔有可能返回true值。
擴展:equals方法具有對稱性,a.equals(b)和b.equals(a)效果一樣。
注意:如果是一個變量和一個常量進行比較,那麼強烈建議將常量寫在前面。
public boolean equalsIgnoreCase(String str):讓當前字符串和另一個參數字符串進行比較,忽略大小寫。
*/
public class Demo03StringEquals {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = "hello";
boolean same = str1.equals(str2);
System.out.println(same); // true
System.out.println(str2.equals(str3)); // false
System.out.println("===========");
String str4 = null;
System.out.println("hello".equals(str4)); // false,推薦這麼寫
// System.out.println(str4.equals("hello")); // NullPointerException,有安全隱患
System.out.println("===========");
System.out.println(str2.equalsIgnoreCase(str3)); // true
// 注意!只有英文字母纔有大小寫之分,其他的沒有大小寫
System.out.println("HelloWorld123".equalsIgnoreCase("helloworld123")); // true
System.out.println("HelloWorld12".equalsIgnoreCase("helloworld123")); // false
System.out.println("壹元錢".equalsIgnoreCase("一元錢")); // false
}
}
字符串當中與獲取相關的常用方法有:
public int length():獲取字符串的長度,也就是其中包含的字符個數。
public String concat(String another):將參數字符串拼到結尾位置,返回新字符串。
public char charAt(int index):返回得到指定索引位置的單個字符。
public int indexOf(String str):在當前字符串當中查找參數字符串第一次出現的索引位置,如果沒有返回值-1
*/
public class Demo04StringGet {
public static void main(String[] args) {
String str = "qervwefqrwfqrwrcervererwfaew";
System.out.println(str.length()); // 28
System.out.println("中國".length()); // 2
System.out.println("=============");
String str1 = "abc";
String str2 = str1.concat("123");
System.out.println(str1); // abc
System.out.println(str2); // abc123
System.out.println("=============");
char ch = "HelloWorld".charAt(5);
System.out.println(ch); // W
System.out.println("=============");
System.out.println("HelloWorld".indexOf("llo")); // 2
System.out.println("HelloWorldHello".indexOf("llo")); // 仍然是2,只看第一次
System.out.println("Hello".indexOf("abc")); // -1
}
}
}