Java String equalsIgnoreCase() method

The equalsIgnoreCase() method of the String class compares two strings based on their contents irrespective of the case ( lowercase or uppercase ).

  • Method declaration – public boolean equalsIgnoreCase(String anotherString).
  • What does it do? – It will accept a string as a function argument which will be compared with the string calling the function.
  • What does it return? It will return true if and only if both of the strings are equal irrespective of the case. Otherwise, it will return false.

Here, the function will be matching the strings character by character to check whether their contents are equal or not. And it would consider two characters equal irrespective of the case if they satisfy at least one of the below conditions –

  • The two characters are the same (as compared by the == operator).
  • Calling Character.toLowerCase(Character.toUpperCase(char)) on corresponding characters gives the same result.
Code Example
public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";
		String str2 = "codekru";
		String str3 = "hello";
		
		System.out.println("is str1 equals to str2 ignoring cases? " + str1.equalsIgnoreCase(str2));
		System.out.println("is str1 equals to str3 ignoring cases? " + str1.equalsIgnoreCase(str3));

	}
}

Output –

is str1 equals to str2 ignoring cases? true
is str1 equals to str3 ignoring cases? false

The What If scenarios

What if we used the equalsIgnoreCase() on empty strings?

Here also, the function will return true.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "";
		String str2 = "";
		
		System.out.println("is str1 equals to str2 ignoring cases? " + str1.equalsIgnoreCase(str2));

	}
}

Output –

is str1 equals to str2 ignoring cases? true
What if we try to use equalsIgnoreCase() on a null string?

Here it would give us NullPointerException.

public class Codekru {

	public static void main(String[] args) {

		String str1 = null;
		String str2 = "codekru";
		
		
		System.out.println("is str1 equals to str2 ignoring cases? " + str1.equalsIgnoreCase(str2));

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.equalsIgnoreCase(String)" because "str1" is null
What if we passed a null string in the function’s argument?

It will return false and will not throw any exception.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";
		String str2 = null;
		
		
		System.out.println("is str1 equals to str2 ignoring cases? " + str1.equalsIgnoreCase(str2));

	}
}

Output –

is str1 equals to str2 ignoring cases? false

Time complexity

The time complexity of the equalsIgnoreCase() function is O(n), where n is the length of the string.

O(n) is the worst-case time complexity. Below scenarios would result in O(n) complexity –

  • When both strings are equal.
  • And when only the last character of the string mismatches.

In both scenarios, equalsIgnoreCase() must iterate over the whole string.

But there are certain scenarios where the complexity could be O(1). Below are some of them.

  • If we passed the same string into the function’s argument we are comparing, it would return true in O(1) time.
  • If we compare strings of two different lengths, it would also return false in O(1) time. Because the function checks the length of both strings and proceeds only if the length of both strings is equal.
  • The first character of the comparing strings does not match.

If you want to know more about strings and their methods, we recommend reading this article.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.