Case-Insensitive String Comparison in Java
Fast recall
Quick revision
Core answer
equalsIgnoreCase() for a simple locale-independent equality check and compareToIgnoreCase() for a similar ordering. They are not locale-sensitive collation. For case-insensitive technical keys, normalize with toLowerCase(Locale.ROOT) when normalization is appropriate; for user-facing language rules, use Collator or a domain-specific comparison. Java has no direct case-insensitive contains() method.Whether you know equalsIgnoreCase() and can explain the normalisation pattern for methods that lack a case-insensitive variant.
Manually comparing lowercased strings with == instead of .equals(). Or converting only one side to lowercase when using contains().
Mention that toLowerCase() and toUpperCase() should use Locale.ROOT in production to avoid the Turkish locale issue where 'i'.toUpperCase() produces 'İ' instead of 'I'.
Interview-ready guide
Interview answer
For a simple case-insensitive equality check I use equalsIgnoreCase(). If I need stable normalized technical keys, I can lower-case both values with Locale.ROOT. I would not use the machine's default locale because the result can change between environments.
For user-facing language sorting, compareToIgnoreCase() is not enough; I would use Collator for the target locale. Java also has no direct ignore-case contains, so both sides need the same clearly defined normalization or a library operation.
Case-Insensitive String Comparison in Java
Direct Answer
Use equalsIgnoreCase() for a simple locale-independent equality check and compareToIgnoreCase() for a similar ordering. They are not locale-sensitive collation. For case-insensitive technical keys, normalize with toLowerCase(Locale.ROOT) when normalization is appropriate; for user-facing language rules, use Collator or a domain-specific comparison. Java has no direct case-insensitive contains() method.
Code Example
import java.util.Locale;
public class CaseInsensitiveDemo {
public static void main(String[] args) {
// equalsIgnoreCase — built-in, no transformation needed
System.out.println("Hello".equalsIgnoreCase("hello")); // true
System.out.println("JAVA".equalsIgnoreCase("java")); // true
System.out.println("Java".equalsIgnoreCase("javascript")); // false
// compareToIgnoreCase — ordering, case ignored
System.out.println("Apple".compareToIgnoreCase("apple")); // 0 — equal
System.out.println("Apple".compareToIgnoreCase("Banana")); // negative — apple < banana
// contains/startsWith/endsWith — no built-in, normalise both sides
String url = "https://www.Example.com";
String search = "EXAMPLE";
// WRONG — only one side normalised
System.out.println(url.toLowerCase().contains(search)); // false!
// CORRECT — both sides normalised
System.out.println(url.toLowerCase().contains(search.toLowerCase())); // true
// Production-safe with Locale.ROOT
System.out.println(
url.toLowerCase(Locale.ROOT).contains(search.toLowerCase(Locale.ROOT))
); // true — avoids Turkish locale issue
}
}
Common Mistakes & Pitfalls
- Lowercasing then comparing with
==—a.toLowerCase() == b.toLowerCase()compares references and is almost alwaysfalse; useequalsIgnoreCase()or.equals(). - Normalising only one side for search —
url.toLowerCase().contains(term)misses whentermhas capitals; lowercase both sides. - Using default-locale
toLowerCase()on a server — the Turkish locale turns'i'into'İ', breaking matches; passLocale.ROOT. - Calling
equalsIgnoreCase()on anullreference — it still throws if the caller is null; put a known-non-null String first or guard the value.
Check your understanding
Answer these without looking back at the article.
- What is the Turkish locale issue with String.toLowerCase()?
- How would you implement a case-insensitive TreeSet in Java?
- What is the difference between equalsIgnoreCase() and equals() on a lowercased string?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.