Java字符串比較
String類覆蓋了Object類的equals()方法,並提供了自己的實現,equals()方法是根據它們的內容比較兩個字符串的相等性。
等於(相等)
例如,可以比較兩個字符串的相等性,如下所示:
String str1 = new String("Hello");
String str2 = new String("Hi");
String str3 = new String("Hello");
boolean b1, b2;
b1 = str1.equals(str2); // false will be assigned to b1
b2 = str1.equals(str3); // true will be assigned to b2
還可以將字符串字面量與字符串字面量,或字符串對象進行比較,如下所示:
b1 = str1.equals("Hello"); // true will be assigned to b1
b2 = "Hello".equals(str1); // true will be assigned to b2
b1 = "Hello".equals("Hi"); // false will be assigned to b1
==操作符總是比較內存中兩個對象的引用。str1 == str2和str1 == str3將返回false,因爲str1,str2和str3是內存中三個不同String對象的引用。
比較
要根據字符的Unicode值比較兩個字符串,請使用compareTo()方法。 它的簽名是 -
public int compareTo(String anotherString)
它返回一個整數,它可以是0(零),正整數或負整數。此方法返回這兩個字符的Unicode值之間的差值。
例如,"a".compareTo("b")將返回-1。 a的Unicode值爲97,b爲98。它返回差值97 - 98,所以它返回的差值是:-1。
以下是字符串比較的示例:
"abc".compareTo("abc") will return 0
"abc".compareTo("xyz") will return -23 (value of 'a' - 'x')
"xyz".compareTo("abc") will return 23 (value of 'x' - 'a')
以下代碼顯示如何進行字符串比較。
public class Main {
public static void main(String[] args) {
String apple = new String("Apple");
String orange = new String("Orange");
System.out.println(apple.equals(orange));
System.out.println(apple.equals(apple));
System.out.println(apple == apple);
System.out.println(apple == orange);
System.out.println(apple.compareTo(apple));
System.out.println(apple.compareTo(orange));
}
}
上面的代碼生成以下結果。
false
true
true
false
0
-14
字符串池
Java維護一個所有字符串文字的池。它在字符串池中爲每個字符串文字創建一個String對象。當遇到字符串字面量時,它在字符串池中查找具有相同內容的字符串對象。 如果在字符串池中找不到匹配項,它將創建一個新的String對象並將其添加到字符串池中。
如果它在字符串池中找到匹配項,它將使用池中找到的String對象的引用替換字符串字面值。
可以使用intern()方法向字符串池添加一個String對象。
如果找到匹配,intern()方法從字符串池返回對象的引用。 否則,它將一個新的String對象添加到字符串池,並返回新對象的引用。
字符串大小寫比較
要比較兩個字符串是否相等並忽略它們的大小,請使用equalsIgnoreCase()方法。要對兩個字符串執行區分大小寫的比較,請使用equals()方法。
public class Main {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "HELLO";
if (str1.equalsIgnoreCase(str2)) {
System.out.println("Ignoring case str1 and str2 are equal");
} else {
System.out.println("Ignoring case str1 and str2 are not equal");
}
if (str1.equals(str2)) {
System.out.println("str1 and str2 are equal");
} else {
System.out.println("str1 and str2 are not equal");
}
}
}
上面的代碼生成以下結果。
Ignoring case str1 and str2 are equal
str1 and str2 are not equal
語言敏感字符串比較
String類根據字符的Unicode值比較字符串。要根據字典順序比較字符串,請使用java.text.Collator類的compare()方法執行語言敏感(字典順序)字符串比較。
該方法需要兩個字符串作爲參數進行比較。 如果兩個字符串相同,返回0,如果第一個字符串在第二個字符串之後返回1,如果第一個字符串在第二個字符串之前,返回-1。
以下代碼說明了Collator類的使用。
import java.text.Collator;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
Locale USLocale = new Locale("en", "US");
Collator c = Collator.getInstance(USLocale);
String str1 = "Java";
String str2 = "HTML";
int diff = c.compare(str1, str2);
System.out.print("Comparing using Collator class: ");
if (diff > 0) {
System.out.println(str1 + " comes after " + str2);
} else if (diff < 0) {
System.out.println(str1 + " comes before " + str2);
} else {
System.out.println(str1 + " and " + str2 + " are the same.");
}
}
}
上面的代碼生成以下結果。
Comparing using Collator class: Java comes after HTML