Java Language String Comparison


Compare two Strings ignoring case:

"School".equalsIgnoreCase("school"); // true

Don't use


Languages have different rules for converting upper and lower case. A 'I' would be converted to 'i' in English. But in Turkish a 'I' becomes a 'ı'. If you have to use toLowerCase() use the overload which expects a Locale: String.toLowerCase(Locale).

Comparing two Strings ignoring minor differences:

Collator collator = Collator.getInstance(Locale.GERMAN);
collator.equals("Gärten", "gaerten"); // returns true

Sort Strings respecting natural language order, ignoring case (use collation key to:

String[] texts = new String[] {"Birne", "äther", "Apfel"};
Collator collator = Collator.getInstance(Locale.GERMAN);
collator.setStrength(Collator.SECONDARY); // ignore case
Arrays.sort(texts, collator::compare); // will return {"Apfel", "äther", "Birne"}