.equalsIgnoreCase()

Christine_Yang's avatar
Published Feb 12, 2023Updated Feb 23, 2023
Contribute to Docs

The .equalsIgnoreCase() method compares two strings ignoring lower/upper case differences and returns true if the two are the same. Otherwise, it returns false.

Syntax

boolean result = stringOne.equalsIgnoreCase(stringTwo);
  • result is a boolean type variable that stores the output.
  • stringOne and stringTwo are the two strings being compared.

Example

The example below uses the .equalsIgnoreCase() method to compare the firstString, secondString, and thirdString strings. The results are stored in firstResult and secondResult and then printed out:

public class CompareStrings {
public static void main(String[] args) {
//Declare strings
String firstString = "welcome";
String secondString = "WELcome";
String thirdString = "Hello there";
// Compare firstString and secondString
boolean firstResult = firstString.equalsIgnoreCase(secondString);
System.out.println("Is the first string equal to the second string? The answer is: " + firstResult);
//Compare secondString and thirdString
boolean secondResult = secondString.equalsIgnoreCase(thirdString);
System.out.println("Is the second string equal to the third string? The answer is: " + secondResult);
}
}

This will output the following result:

Is the first string equal to the second string? The answer is: true
The example above will have the output: Is the second string equal to the third string? The answer is: false

All contributors

Contribute to Docs

Learn Java on Codecademy