Locale-Sensitive String Comparison
In the Internationalization Module,
RWUCollator performs locale-sensitive string comparison. Each
RWUCollator is associated with an
RWULocale at construction time, in order to take language-specific conventions into account. For example.
RWULocale myLocale("de");
RWUCollator myCollator(myLocale);
If no locale is specified, then the current default locale is used. The locale specifies the default values for a variety of
RWUCollator attributes. Collator attributes can be overridden using mutator methods (
Customizing a Collator). The
getLocale() method returns the locale upon which a collator is based.
Once you’ve instantiated an
RWUCollator, you can compare two strings according to the dictates of the collator’s attributes using the
compareTo() method:
int retval = collator.compareTo(str1, str2);
The compareTo() method returns:
-1 if
str1 < str2 0 if
str1 == str2 1 if
str1 > str2 For example, coté compares less than côte in the English locale, but greater than côte in the French locale:
RWUConversionContext context("Latin1");
RWUString s1("coté");
RWUString s2("côte");
RWULocale en(RWCString("en"));
RWUCollator enCollator(en); //1
cout << "\nEnglish locale: " << s1;
if (enCollator.compareTo(s1, s2) < 0) { //2
cout << " is";
} else {
cout << " is NOT";
} // else
cout << " less than " << s2 << "." << endl;
RWULocale fr(RWCString("fr"));
RWUCollator frCollator(fr); //3
cout << "\nFrench locale: " << s1;
if (frCollator.compareTo(s1, s2) < 0) { //4
cout << " is";
} else {
cout << " is NOT";
} // else
cout << " less than " << s2 << << endl;
The output is:
English locale: coté is less than côte
French locale: coté is NOT less than côte
Similarly, the equals() method returns true if two strings compare equal according to the dictates of the collator’s attributes; otherwise, false. Thus:
if ( collator.equals(str1, str2) ) {
// Do something here...
}