使用 equalsIgnoreCase()
进行不区分大小写的比较
在这最后一步中,我们将学习 equalsIgnoreCase()
方法,该方法用于比较字符串,而不考虑字符的大小写。当你想检查两个字符串是否相同,而不考虑字母是大写还是小写时,这个方法非常有用。
我们在前面步骤中使用的 equals()
方法进行的是区分大小写的比较。这意味着使用 equals()
时,"hello" 不等于 "Hello"。而 equalsIgnoreCase()
方法会忽略大小写,因此 "hello" 和 "Hello" 会被视为相等。
让我们最后一次修改我们的程序,来演示 equalsIgnoreCase()
方法的使用。
-
在 WebIDE 编辑器中打开 StringComparison.java
文件。
-
添加一些大小写不同的新字符串变量,并使用 equalsIgnoreCase()
方法进行比较:
public class StringComparison {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "hello";
String str3 = new String("hello");
String str4 = null;
String str5 = "Hello"; // Different casing
String str6 = "HELLO"; // All uppercase
System.out.println("Comparing str1 and str2 using equals(): " + str1.equals(str2));
System.out.println("Comparing str1 and str3 using equals(): " + str1.equals(str3));
System.out.println("Comparing str1 and str2 using ==: " + (str1 == str2));
System.out.println("Comparing str1 and str3 using ==: " + (str1 == str3));
// Safe comparison with null
System.out.println("Comparing str1 and str4 safely: " + (str1 != null && str1.equals(str4)));
System.out.println("Comparing str4 and str1 safely: " + (str4 != null && str4.equals(str1)));
System.out.println("Comparing str4 and null safely: " + (str4 == null));
System.out.println("\n--- Case-Insensitive Comparisons ---");
System.out.println("Comparing str1 and str5 using equals(): " + str1.equals(str5));
System.out.println("Comparing str1 and str5 using equalsIgnoreCase(): " + str1.equalsIgnoreCase(str5));
System.out.println("Comparing str1 and str6 using equalsIgnoreCase(): " + str1.equalsIgnoreCase(str6));
System.out.println("Comparing str5 and str6 using equalsIgnoreCase(): " + str5.equalsIgnoreCase(str6));
// equalsIgnoreCase() also handles null safely if called on a non-null string
System.out.println("Comparing str1 and str4 using equalsIgnoreCase(): " + str1.equalsIgnoreCase(str4));
// System.out.println("Comparing str4 and str1 using equalsIgnoreCase(): " + str4.equalsIgnoreCase(str1)); // This would still cause a NullPointerException
}
}
在更新后的代码中:
- 我们添加了大小写不同的
str5
和 str6
。
- 我们添加了新的打印语句,以展示
equals()
和 equalsIgnoreCase()
方法的区别。
- 我们还展示了在非空字符串上调用
equalsIgnoreCase()
方法并传入 null
参数不会导致错误,这与 equals()
方法类似。然而,在 null
字符串变量上调用 equalsIgnoreCase()
方法仍然会导致 NullPointerException
。
-
保存文件(Ctrl+S 或 Cmd+S)。
-
在终端中编译程序:
javac StringComparison.java
-
运行编译后的程序:
java StringComparison
你应该会看到类似以下的输出:
Comparing str1 and str2 using equals(): true
Comparing str1 and str3 using equals(): true
Comparing str1 and str2 using ==: true
Comparing str1 and str3 using ==: false
Comparing str1 and str4 safely: false
Comparing str4 and str1 safely: false
Comparing str4 and null safely: true
--- Case-Insensitive Comparisons ---
Comparing str1 and str5 using equals(): false
Comparing str1 and str5 using equalsIgnoreCase(): true
Comparing str1 and str6 using equalsIgnoreCase(): true
Comparing str5 and str6 using equalsIgnoreCase(): true
Comparing str1 and str4 using equalsIgnoreCase(): false
输出清晰地表明,在比较字符相同但大小写不同的字符串时,equalsIgnoreCase()
方法返回 true
,而 equals()
方法返回 false
。
现在你已经学会了如何在 Java 中使用 equals()
方法进行区分大小写的字符串比较,如何安全地处理 null
字符串,以及如何使用 equalsIgnoreCase()
方法进行不区分大小写的字符串比较。这些都是在 Java 中处理字符串的必备技能。