Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java String equalsIgnoreCase() method
Syntax and Examples



Introduction

Sometimes 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.

Syntax


public boolean equalsIgnoreCase(String anotherString)

Parameters

ParameterDescription
anotherStringThe string to compare with the current String object.

Return Value

This method returns true if the strings are equal, ignoring case; otherwise, it returns false.

Examples

Example 1: Basic Comparison

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.

Example 2: Comparing with Null

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.

Example 3: Comparing Strings with Different Characters

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.

Example 4: Empty Strings

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().

Example 5: Comparing with Special Characters

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.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You 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.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M