Java Reference
In-Depth Information
24.6. Internationalization and Localization for Text
The package
java.text
provides several types for localizing text behavior,
such as collation (comparing strings), and formatting and parsing text,
numbers, and dates. You have already learned about dates in detail so in
this section we look at general formatting and parsing, and collation.
24.6.1. Collation
Comparing strings in a locale-sensitive fashion is called
collation.
The
central class for collation is
Collator
, which provides a
compare
method
that takes two strings and returns an
int
less than, equal to, or greater
than zero as the first string is less than, equal to, or greater than the
second.
As with most locale-sensitive classes, you get the best available
Collator
object for a locale from a
getInstance
method, either passing a specific
Locale
object or specifying no locale and so using the default locale. For
example, you get the best available collator to sort a set of Russian-lan-
guage strings like this:
Locale russian = new Locale("ru", "");
Collator coll = Collator.getInstance(russian);
You then can use
coll.compare
to determine the order of strings. A
Collat-
or
object takes localitynot Unicode equivalenceinto account when com-
paring. For example, in a French-speaking locale, the characters
ç
and
c
are considered equivalent for sorting purposes. A naïve sort that used
String.compare
would put all strings starting with
ç
after all those start-
ing with
c
(indeed, it would put them after
z
), but in a French locale this
would be wrong. They should be sorted according to the characters that
follow the initial
c
or
ç
characters in the strings.