CompareInfo::Compare Method (String^, String^, CompareOptions)
Compares two strings using the specified CompareOptions value.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- string1
-
Type:
System::String^
The first string to compare.
- string2
-
Type:
System::String^
The second string to compare.
- options
-
Type:
System.Globalization::CompareOptions
A value that defines how string1 and string2 should be compared. options is either the enumeration value Ordinal, or a bitwise combination of one or more of the following values: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, IgnoreKanaType, and StringSort.
Return Value
Type: System::Int32A 32-bit signed integer indicating the lexical relationship between the two comparands.
Value | Condition |
|---|---|
zero | The two strings are equal. |
less than zero | string1 is less than string2. |
greater than zero | string1 is greater than string2. |
| Exception | Condition |
|---|---|
| ArgumentException | options contains an invalid CompareOptions value. |
If a security decision depends on a string comparison or a case change, you should use the InvariantCulture property to ensure that the behavior is consistent regardless of the culture settings of the operating system.
Note |
|---|
When possible, you should call string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify Ordinal or OrdinalIgnoreCase for security comparisons. |
Notes to Callers:
Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. The Compare(String^, String^, CompareOptions) method does not consider such characters when it performs a culture-sensitive comparison. To recognize ignorable characters in your comparison, supply a value of CompareOptions::Ordinal or CompareOptions::OrdinalIgnoreCase for the options parameter.
The following example compares two strings using different CompareOptions settings.
using namespace System; using namespace System::Globalization; int main() { // Defines the strings to compare. String^ myStr1 = "My Uncle Bill's clients"; String^ myStr2 = "My uncle bills clients"; // Creates a CompareInfo which uses the InvariantCulture. CompareInfo^ myComp = CultureInfo::InvariantCulture->CompareInfo; // Compares two strings using myComp. Console::WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1, myStr2 ); Console::WriteLine( " With no CompareOptions : {0}", myComp->Compare( myStr1, myStr2 ) ); Console::WriteLine( " With None : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::None ) ); Console::WriteLine( " With Ordinal : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::Ordinal ) ); Console::WriteLine( " With StringSort : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::StringSort ) ); Console::WriteLine( " With IgnoreCase : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::IgnoreCase ) ); Console::WriteLine( " With IgnoreSymbols : {0}", myComp->Compare( myStr1, myStr2, CompareOptions::IgnoreSymbols ) ); Console::WriteLine( " With IgnoreCase and IgnoreSymbols : {0}", myComp->Compare( myStr1, myStr2, static_cast<CompareOptions>(CompareOptions::IgnoreCase | CompareOptions::IgnoreSymbols) ) ); } /* This code produces the following output. Comparing "My Uncle Bill's clients" and "My uncle bills clients" With no CompareOptions : 1 With None : 1 With Ordinal : -32 With StringSort : -1 With IgnoreCase : 1 With IgnoreSymbols : 1 With IgnoreCase and IgnoreSymbols : 0 */
The following example demonstrates calling the Compare method.
using namespace System; using namespace System::Text; using namespace System::Globalization; int main() { array<String^>^ sign = gcnew array<String^> { "<", "=", ">" }; // The code below demonstrates how strings compare // differently for different cultures. String^ s1 = "Coté"; String^ s2 = "coté"; String^ s3 = "côte"; // Set sort order of strings for French in France. CompareInfo^ ci = (gcnew CultureInfo("fr-FR"))->CompareInfo; Console::WriteLine(L"The LCID for {0} is {1}.", ci->Name, ci->LCID); // Display the result using fr-FR Compare of Coté = coté. Console::WriteLine(L"fr-FR Compare: {0} {2} {1}", s1, s2, sign[ci->Compare(s1, s2, CompareOptions::IgnoreCase) + 1]); // Display the result using fr-FR Compare of coté > côte. Console::WriteLine(L"fr-FR Compare: {0} {2} {1}", s2, s3, sign[ci->Compare(s2, s3, CompareOptions::None) + 1]); // Set sort order of strings for Japanese as spoken in Japan. ci = (gcnew CultureInfo("ja-JP"))->CompareInfo; Console::WriteLine(L"The LCID for {0} is {1}.", ci->Name, ci->LCID); // Display the result using ja-JP Compare of coté < côte. Console::WriteLine("ja-JP Compare: {0} {2} {1}", s2, s3, sign[ci->Compare(s2, s3) + 1]); } // This code produces the following output. // // The LCID for fr-FR is 1036. // fr-FR Compare: Coté = coté // fr-FR Compare: coté > côte // The LCID for ja-JP is 1041. // ja-JP Compare: coté < côte
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
