equalsIgnoreCase() を使用した大文字小文字を区別しない比較
この最後のステップでは、equalsIgnoreCase()
メソッドについて学びます。このメソッドは、文字の大文字小文字を考慮せずに文字列を比較するために使用されます。文字が大文字か小文字かに関係なく、2 つの文字列が同じかどうかをチェックしたい場合に非常に便利です。
前のステップで使用した 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()
の違いを実証するための新しい出力文を追加しました。
equals()
と同様に、非 null
文字列に対して null
引数で equalsIgnoreCase()
を呼び出してもエラーは発生しないことを示しています。ただし、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 で文字列を扱う上で不可欠なスキルです。