CompareInfo.Compare Method (String, Int32, String, Int32)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Compares the end section of a string with the end section of another string 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.
- offset1
- Type: System.Int32
The zero-based index of the character in string1 at which to start comparing.
- string2
- Type: System.String
The second string to compare.
- offset2
- Type: System.Int32
The zero-based index of the character in string2 at which to start comparing.
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 | The specified section of string1 is less than the specified section of string2. |
greater than zero | The specified section of string1 is greater than the specified section of string2. |
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | offset1 or offset2 is less than zero. -or- offset1 is greater than or equal to the number of characters in string1. -or- offset2 is greater than or equal to the number of characters in string2. |
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.
The following example compares portions of two strings using the different CompareInfo objects:
CompareInfo object associated with the Spanish (Spain) culture with international sort
CompareInfo object associated with the Spanish (Spain) culture with traditional sort
CompareInfo object associated with the InvariantCulture
using System; using System.Globalization; public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // Define the strings to compare. string string1 = "coté"; string string2 = "côte"; // Get CompareInfo objects for three cultures. CompareInfo compareInv = CultureInfo.InvariantCulture.CompareInfo; CompareInfo compareEnUS = CompareInfo.GetCompareInfo("en-US"); CompareInfo compareFrFR = CompareInfo.GetCompareInfo("fr-FR"); // Compare the two strings using each CompareInfo object. outputBlock.Text += String.Format("Comparing \"{0}\" and \"{1}\"\n", string1.Substring(1), string2.Substring(1)); outputBlock.Text += String.Format(" With fr-FR Culture: {0}\n", compareFrFR.Compare(string1, 1, string2, 1)); outputBlock.Text += String.Format(" With en-US Culture: {0}\n", compareEnUS.Compare(string1, 1, string2, 1)); outputBlock.Text += String.Format(" With Invariant Culture: {0}\n", compareInv.Compare(string1, 1, string2, 1)); } } /* This example produces the following output: Comparing "oté" and "ôte" With fr-FR Culture: 1 With en-US Culture: -1 With Invariant Culture: -1 */