⬅ Previous Topic
Java Stringequals()
methodNext Topic ⮕
Java Stringformat()
methodequalsIgnoreCase()
method⬅ Previous Topic
Java Stringequals()
methodNext Topic ⮕
Java Stringformat()
methodSometimes you need to compare strings but want to ignore differences in capitalization. For example, "Hello" and "hello" should be considered the same. The equalsIgnoreCase()
method provides a convenient way to do this in Java.
public boolean equalsIgnoreCase(String anotherString)
Parameter | Description |
---|---|
anotherString | The string to compare with the current String object. |
This method returns true
if the strings are equal, ignoring case; otherwise, it returns false
.
Let's start with a simple example. We’ll compare two strings, one in lowercase and one in uppercase, to see if the method correctly ignores capitalization.
String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equalsIgnoreCase(str2);
System.out.println(isEqual);
true
In this example, str1
is initialized to "Hello" and str2
to "hello". The equalsIgnoreCase()
method compares these two strings. Since they are the same string ignoring case, the result is stored in isEqual
as true
, which is then printed to the console.
It's important to consider how equalsIgnoreCase()
behaves when one of the strings being compared is null. Attempting to call methods on a null reference throws a NullPointerException
. Let's see what happens.
String str1 = "Hello";
String str2 = null;
//This will throw NullPointerException
//boolean isEqual = str1.equalsIgnoreCase(str2);
System.out.println("Avoiding NullPointerException.");
Avoiding NullPointerException.
The code above is commented out because attempting to call equalsIgnoreCase()
on a non-null string with a null argument will cause a NullPointerException
. Always check for null values before calling this method.
Now, let's look at an example where the strings are different in more than just capitalization. This demonstrates that case-insensitive comparison does not mean identical content.
String str1 = "Hello World";
String str2 = "hello world!";
boolean isEqual = str1.equalsIgnoreCase(str2);
System.out.println(isEqual);
false
Here, str1
is set to "Hello World" and str2
to “hello world!”. Even though the capitalization differs, the presence of an exclamation mark in str2
makes them different strings. Therefore, the method returns false
.
Let's see how the comparison works with empty strings.
String str1 = "";
String str2 = "";
boolean isEqual = str1.equalsIgnoreCase(str2);
System.out.println(isEqual);
true
In this example, both strings are empty. An empty string is considered equal to another empty string when using equalsIgnoreCase()
.
This example demonstrates how special characters affect the comparison.
String str1 = "Test@123";
String str2 = "test@123";
boolean isEqual = str1.equalsIgnoreCase(str2);
System.out.println(isEqual);
true
The equalsIgnoreCase()
method ignores case but considers all other characters, including special ones like the '@' symbol. Since both strings have identical content when case is ignored, the comparison returns true.
⬅ Previous Topic
Java Stringequals()
methodNext Topic ⮕
Java Stringformat()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.