Export (0) Print
Expand All

CompareInfo.Compare Method (String, Int32, String, Int32)

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.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

public virtual int Compare(
	string string1,
	int offset1,
	string string2,
	int offset2
)

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.Int32
An 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.

ExceptionCondition
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:


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
*/


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft