CompareInfo.Compare Method (String, String, CompareOptions)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Compares two strings using the specified CompareOptions value and returns an integer that indicates their relationship to one another in the sort order.
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
The CompareOptions value that defines how string1 and string2 should be compared. options is either the value Ordinal used by itself, or the bitwise combination of one or more of the following values: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, IgnoreKanaType, and StringSort.
Return Value
Type: System.Int32An integer that indicates the relationship between the two strings in the sort order, as follows:
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, the application should use the InvariantCulture to ensure that the behavior is consistent regardless of the culture settings of the operating system.
Version Notes
Windows Phone
The Compare method does not throw the expected exception, ArgumentOutOfRangeException, if you pass an invalid CompareOptions object.The following example compares two strings using different CompareOptions settings.
using System; using System.Globalization; public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // Defines the strings to compare. String myStr1 = "My Uncle Bill's clients"; String myStr2 = "My uncle bills clients"; // Creates a CompareInfo that uses the InvariantCulture. CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo; // Compares two strings using myComp. outputBlock.Text += String.Format("Comparing \"{0}\" and \"{1}\"", myStr1, myStr2) + "\n"; outputBlock.Text += String.Format(" With no CompareOptions : {0}", myComp.Compare(myStr1, myStr2)) + "\n"; outputBlock.Text += String.Format(" With None : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.None)) + "\n"; outputBlock.Text += String.Format(" With Ordinal : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.Ordinal)) + "\n"; outputBlock.Text += String.Format(" With StringSort : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.StringSort)) + "\n"; outputBlock.Text += String.Format(" With IgnoreCase : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreCase)) + "\n"; outputBlock.Text += String.Format(" With IgnoreSymbols : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreSymbols)) + "\n"; outputBlock.Text += String.Format(" With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols)) + "\n"; } } /* 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 */