HOWTO · Java
How to Ignore Upper Case and Lower Case in Java
This article introduces how to ignore upper case and lower case in Java.
On this page
To test whether two Java strings are equal while ignoring letter case, call equalsIgnoreCase(). Use toUpperCase() or toLowerCase() followed by equals() when you need normalized strings for additional processing. For case-insensitive ordering, use compareToIgnoreCase().
A Java String is immutable, so toUpperCase() and toLowerCase() return a String containing the converted content while leaving the original value unchanged. These methods and equalsIgnoreCase() use Java’s case mappings; they do not provide full language-specific Unicode case folding. Choose the method that matches whether you need equality, normalized values, or ordering.
Ignore Case Using toUpperCase() Method in Java
Convert both strings with toUpperCase(Locale.ROOT), then compare the results with equals(). Locale.ROOT makes the conversion independent of the machine’s default locale, which is useful for programmatic normalization such as identifiers. Ordinary ASCII examples do not require an English or US default locale.
import java.util.Locale;
public class UppercaseComparison {
public static void main(String[] args) {
String first = "icecream";
String second = "IceCream";
String normalizedFirst = first.toUpperCase(Locale.ROOT);
String normalizedSecond = second.toUpperCase(Locale.ROOT);
boolean equalIgnoringCase = normalizedFirst.equals(normalizedSecond);
System.out.println(equalIgnoringCase);
}
}
This approach compares the returned String values after conversion. Use Locale.ROOT when the conversion is for machine-to-machine comparison; locale-aware text processing may need a different design.
Ignore Case Using toLowerCase() Method in Java
Lowercase normalization follows the same pattern: convert both strings with toLowerCase(Locale.ROOT), then compare them with equals(). The explicit locale prevents results from depending on the user’s default locale. Use this approach when later operations also need the normalized lowercase values; for a direct equality check, equalsIgnoreCase() is simpler.
import java.util.Locale;
public class LowercaseComparison {
public static void main(String[] args) {
String first = "icecream";
String second = "IceCream";
String normalizedFirst = first.toLowerCase(Locale.ROOT);
String normalizedSecond = second.toLowerCase(Locale.ROOT);
boolean equalIgnoringCase = normalizedFirst.equals(normalizedSecond);
System.out.println(equalIgnoringCase);
}
}
As with uppercase conversion, toLowerCase() returns a String containing the converted content while leaving the original value unchanged; it is not a substitute for language-specific Unicode case folding.
Ignore Case Using equalsIgnoreCase() Method in Java
For a direct equality test, call equalsIgnoreCase() on one string and pass the other. It returns a boolean: true when the strings are equal under Java’s case-insensitive comparison, and false otherwise. For example, "icecream".equalsIgnoreCase("IceCream") is true.
public class IgnoreCaseEquality {
public static void main(String[] args) {
String first = "icecream";
String second = "IceCream";
boolean equalIgnoringCase = first.equalsIgnoreCase(second);
System.out.println(equalIgnoringCase);
}
}
The comparison is not tailored to a particular language or locale and does not perform full Unicode case folding. If an application needs linguistic matching rules, use an API designed for that requirement rather than assuming this method covers every language’s text conventions.
Ignore Case Using compareToIgnoreCase() Method in Java
Use compareToIgnoreCase() when you need lexicographic ordering that ignores case, for example when sorting strings. A result of zero means the strings compare as equal ignoring case. A negative result means the first string sorts before the second, and a positive result means it sorts after. The exact nonzero integer is not guaranteed to be the difference between the first differing characters.
public class IgnoreCaseOrdering {
public static void main(String[] args) {
String first = "apple";
String second = "Banana";
int comparison = first.compareToIgnoreCase(second);
System.out.println(comparison < 0);
}
}
The example prints a boolean for the ordering relationship. Use equalsIgnoreCase() instead when you only need an equality result.