English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Java string equalsIgnoreCase() method compares two strings, ignoring letter case. If the strings are equal, then equalsIgnoreCase() returns true. If they are not equal, it returns false.
The syntax of the string.equalsIgnoreCase() method is:
string.equalsIgnoreCase(String str)
The string equalsIgnoreCase() method takes a single parameter.
str - The string to be compared
If the strings are equal thenReturns trueCase insensitive
If the strings are not equal thenReturns false
If the str parameter is null thenReturns false
class Main { public static void main(String[] args) { String str1 = "Learn Java"; String str2 = "learn java"; String str3 = "Learn Kolin"; Boolean result; //Compare str1and str2 result = str1.equalsIgnoreCase(str2); System.out.println(result); // true //Compare str1and str3 result = str1.equalsIgnoreCase(str3); System.out.println(result); // false //Compare str3and str1 result = str3.equalsIgnoreCase(str1); System.out.println(result); // false } }
Here,
is not case-sensitive, then str1and str2Equal. Therefore, str1.equalsIgnoreCase(str2) returns true.
str1and str3are not equal. Therefore, str1.equalsIgnoreCase(str3) and str3.equalsIgnoreCase(str1) returns false.
class Main { public static void main(String[] args) { String str1 = "LEARN JAVA"; String str2 = "Learn Java"; //If str1and str2Equal (ignoring case differences), //The result is true if (str1.equalsIgnoreCase(str2); System.out.println("str1and str2Equal); } else { System.out.println("str1and str2Not Equal); } } }
Output Result
str1and str2Equal
If you need to compare two strings with case sensitivity, please use one of the following methods