Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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

Compares the end section of a string with the end section of another string using the specified CompareOptions value 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,
	CompareOptions options
)

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

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.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

  The Compare method does not throw the expected exception, ArgumentOutOfRangeException, if you pass an invalid CompareOptions object.

The following example compares portions of 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.Substring(10), myStr2.Substring(10)) + "\n";
      outputBlock.Text += String.Format("   With no CompareOptions            : {0}", myComp.Compare(myStr1, 10, myStr2, 10)) + "\n";
      outputBlock.Text += String.Format("   With None                         : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.None)) + "\n";
      outputBlock.Text += String.Format("   With Ordinal                      : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.Ordinal)) + "\n";
      outputBlock.Text += String.Format("   With StringSort                   : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.StringSort)) + "\n";
      outputBlock.Text += String.Format("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase)) + "\n";
      outputBlock.Text += String.Format("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreSymbols)) + "\n";
      outputBlock.Text += String.Format("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, 10, myStr2, 10, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols)) + "\n";

   }

}


/*
This code produces the following output.

Comparing "ill's clients" and "ills clients"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -76
   With StringSort                   : -1
   With IgnoreCase                   : 1
   With IgnoreSymbols                : 0
   With IgnoreCase and IgnoreSymbols : 0

*/


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:
© 2015 Microsoft