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 (Section 6.3). 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; |
//1 | Creates an RWUCollator based on the English locale. |
//2 | Compares the two strings. |
//3 | Creates an RWUCollator based on the French locale. |
//4 | Compares the two strings. |
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... |
}
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.