Java .equalsIgnoreCase()
Anonymous contributor
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);
resultis a boolean type variable that stores the output.stringOneandstringTwoare 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 stringsString firstString = "welcome";String secondString = "WELcome";String thirdString = "Hello there";// Compare firstString and secondStringboolean firstResult = firstString.equalsIgnoreCase(secondString);System.out.println("Is the first string equal to the second string? The answer is: " + firstResult);//Compare secondString and thirdStringboolean 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: trueThe example above will have the output: Is the second string equal to the third string? The answer is: false
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours