CompareInfo.Compare Method (String, Int32, Int32, String, Int32, Int32)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Compares a section of one string with a section of another string and returns an integer that indicates their relationship to one another in the sort order.
Assembly: mscorlib (in mscorlib.dll)
public virtual int Compare( string string1, int offset1, int length1, string string2, int offset2, int length2 )
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 the comparison.
- length1
- Type: System.Int32
The number of consecutive characters in string1 to compare.
- 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 the comparison.
- length2
- Type: System.Int32
The number of consecutive characters in string2 to compare.
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 precedes the specified section of string2. |
greater than zero | The specified section of string1 follows the specified section of string2. |
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | offset1 or length1 or offset2 or length2 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. -or- length1 is greater than the number of characters from offset1 to the end of string1. -or- length2 is greater than the number of characters from offset2 to the end of string2. |
If a security decision depends on a string comparison, the application should test for quality using ordinal comparison. It should not base a security decision on the Compare method.
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, 3), string2.Substring(1, 3)); outputBlock.Text += String.Format(" With fr-FR Culture: {0}\n", compareFrFR.Compare(string1, 1, 3, string2, 1, 3)); outputBlock.Text += String.Format(" With en-US Culture: {0}\n", compareEnUS.Compare(string1, 1, 3, string2, 1, 3)); outputBlock.Text += String.Format(" With Invariant Culture: {0}\n", compareInv.Compare(string1, 1, 3, string2, 1, 3)); } } /* This example produces the following output: Comparing "oté" and "ôte" With fr-FR Culture: 1 With en-US Culture: -1 With Invariant Culture: -1 */