String Comparison

suggest change

Compare two Strings ignoring case:

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

Don’t use

text1.toLowerCase().equals(text2.toLowerCase());

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.setStrength(Collator.PRIMARY);
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"}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Localization and Internationalization:
* String Comparison
* Locale

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
88 Localization and Internationalization
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap