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