You can set a Collator
's strength property to determine the level of difference considered significant in comparisons. Four strengths are provided: PRIMARY
, SECONDARY
, TERTIARY
, and IDENTICAL
. The exact assignment of strengths to language features is locale dependant. For example, in Czech, "e" and "f" are considered primary differences, while "e" and "ě" are secondary differences, "e" and "E" are tertiary differences and "e" and "e" are identical. The following shows how both case and accents could be ignored for US English.
//Get the Collator for US English and set its strength to PRIMARY Collator usCollator = Collator.getInstance(Locale.US); usCollator.setStrength(Collator.PRIMARY); if( usCollator.compare("abc", "ABC") == 0 ) { System.out.println("Strings are equivalent"); }
For comparing String
s exactly once, the compare
method provides the best performance. When sorting a list of String
s however, it is generally necessary to compare each String
multiple times. In this case, CollationKey
s provide better performance. The CollationKey
class converts a String
to a series of bits that can be compared bitwise against other CollationKey
s. A CollationKey
is created by a Collator
object for a given String
.
Note: CollationKey
s from different Collator
s can not be compared. See the class description for {@link CollationKey}for an example using CollationKey
s.
@see RuleBasedCollator
@see CollationKey
@see CollationElementIterator
@see Locale
@version 1.46, 07/23/06
@author Helena Shih, Laura Werner, Richard Gillam
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|