Export (0) Print
Expand All

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

Compares a section of one string with a section of another string.

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

public virtual int Compare (
	string string1,
	int offset1,
	int length1,
	string string2,
	int offset2,
	int length2
)
public int Compare (
	String string1, 
	int offset1, 
	int length1, 
	String string2, 
	int offset2, 
	int length2
)
public function Compare (
	string1 : String, 
	offset1 : int, 
	length1 : int, 
	string2 : String, 
	offset2 : int, 
	length2 : int
) : int
Not applicable.

Parameters

string1

The first string to compare.

offset1

The zero-based index of the character in string1 at which to start comparing.

length1

The number of consecutive characters in string1 to compare.

string2

The second string to compare.

offset2

The zero-based index of the character in string2 at which to start comparing.

length2

The number of consecutive characters in string2 to compare.

Return Value

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 typeCondition

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 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 code 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 SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "calle";
      String myStr2 = "calor";

      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      CompareInfo myCompIntl = CompareInfo.GetCompareInfo( "es-ES" );
      
      // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      CompareInfo myCompTrad = CompareInfo.GetCompareInfo( 0x040A );

      // Uses the CompareInfo property of the InvariantCulture.
      CompareInfo myCompInva = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myCompIntl.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1.Substring( 2, 2 ), myStr2.Substring( 2, 2 ) );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, 2, 2, myStr2, 2, 2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, 2, 2, myStr2, 2, 2 ) );

   }

}


/*
This code produces the following output.

Comparing "ll" and "lo"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/

import System.* ;
import System.Globalization.* ;

public class SamplesCompareInfo
{
    public static void main(String[] args)
    {
        // Defines the strings to compare.
        String myStr1 = "calle";
        String myStr2 = "calor";

        // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" 
        // culture with international sort.
        CompareInfo myCompIntl = CompareInfo.GetCompareInfo("es-ES");

        // Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" 
        // culture with traditional sort.
        CompareInfo myCompTrad = CompareInfo.GetCompareInfo(0x40A);

        // Uses the CompareInfo property of the InvariantCulture.
        CompareInfo myCompInva = 
            CultureInfo.get_InvariantCulture().get_CompareInfo();

        // Compares two strings using myCompIntl.
        Console.WriteLine("Comparing \"{0}\" and \"{1}\"", 
            myStr1.Substring(2, 2), myStr2.Substring(2, 2));
        Console.WriteLine("   With myCompIntl.Compare: {0}", 
            System.Convert.ToString(myCompIntl.
            Compare(myStr1, 2, 2, myStr2, 2, 2)));
        Console.WriteLine("   With myCompTrad.Compare: {0}", 
            System.Convert.ToString(myCompTrad.
            Compare(myStr1, 2, 2, myStr2, 2, 2)));
        Console.WriteLine("   With myCompInva.Compare: {0}", 
            System.Convert.ToString(myCompInva.
            Compare(myStr1, 2, 2, myStr2, 2, 2)));
    } //main 
} //SamplesCompareInfo

/*
This code produces the following output.

Comparing "ll" and "lo"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft