|
|
Working with Text |
Applications that search or sort through text perform frequent string comparisons. For example, a web browser needs to check for string equality when the end-user searches for text. A report generator performs string comparisons when sorting a list of strings in alphabetical order.If your application audience is limited to people who speak English, you can probably perform string comparisons with the
String.compareTomethod. This method performs a binary comparison of the Unicode characters within the strings. For many languages, this binary comparison cannot be relied upon to sort strings, because the Unicode values do not correspond to the relative order of the characters.Fortunately, the
Collatorclass allows your application to perform string comparisons for different languages. In this lesson, we'll show you how to use the
Collatorclass when searching and sorting through text.Performing Locale-Independent Comparisons
Collation rules define the sort sequence of strings. These rules vary with locale, because various natural languages sort words differently. Using the predefined collation rules provided by theCollatorclass, you can sort strings in a locale-independent manner.Customizing Collation Rules
In some cases, the predefined collation rules provided by theCollatorclass may not work for you. For example, you may want to sort strings in a language whose locale is not supported byCollator. In this situation, you can define your own collation rules, and assign them to aRuleBasedCollatorobject.Improving Collation Performance
With theCollationKeyclass, you may increase the efficiency of string comparisons. This class convertsStringobjects to sort keys that follow the rules of a givenCollator.
|
|
Working with Text |