Locale-Sensitive Java Method
java.lang.String
public int compareToIgnoreCase(String anotherString)
Internationalization (I18n) Method Overview
This method returns an integer whose sign is that of calling compareTo with
normalized versions of the strings where case differences have been eliminated
by calling Character.toLowerCase (Character.toUpperCase(character)) on each
character. The result is a negative integer if this String object
lexicographically precedes the argument string. The result is zero if the
strings are equal; compareToIgnoreCase returns 0 exactly when the
equals(Object) method would return true.
I18n Issues
When this method is being used to compare two strings for the purpose of
collation (alphabetic sorting), it should be replaced with the appropriate call
to the java.text.Collator
class to take advantage of locale-based character sorting.
And to set case insensitivity in the sorting, call the method
setStrength(Collator.SECONDARY) prior to calling Compare .
Globalyzer will not be able to determine whether you are calling this method for
purposes of determining equality or to perform locale-sensitive character
collation. In cases where it is called to determine String equality and hence
you know that the call does not pose an I18n problem, you can use Globalyzer's
Ignore Comment functionality to ensure that it isn't picked up again in
a subsequent scan.
Suggested Replacement
java.text.Collator
public int compare(Object o1,
Object o2)
Instead of:
int i = stringA.compareToIgnoreCase(stringB);
Use:
//retrieve the runtime user's locale
Locale locale = new Locale(getUserLocale());
//pass the user's locale as an argument
Collator myCollator =
Collator.getInstance(locale);
//set collator to Ignore case but not accents
//(default is Collator.TERTIARY, which is
//case sensitive)
myCollator.setStrength(Collator.SECONDARY);
int i = myCollator.compare(stringA,stringB);
Please see Java Collation
for more information.
Locale-Sensitive Java Methods
|