Export (0) Print
Expand All

CompareInfo.Compare Method (String, String)

Compares two strings.

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

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

Parameters

string1

The first string to compare.

string2

The second string to compare.

Return Value

Value

Condition

zero

The two strings are equal.

less than zero

string1 is less than string2.

greater than zero

string1 is greater than string2.

The comparison is performed, by default, using None. 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

// The following code example compares two strings using the different CompareInfo instances:
//    a CompareInfo instance associated with the "Spanish - Spain" culture with international sort,
//    a CompareInfo instance associated with the "Spanish - Spain" culture with traditional sort, and
//    a CompareInfo instance 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, myStr2 );
      Console.WriteLine( "   With myCompIntl.Compare: {0}", myCompIntl.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompTrad.Compare: {0}", myCompTrad.Compare( myStr1, myStr2 ) );
      Console.WriteLine( "   With myCompInva.Compare: {0}", myCompInva.Compare( myStr1, myStr2 ) );

   }

}


/*
This code produces the following output.

Comparing "calle" and "calor"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1

*/

// The following code example compares two strings using the different 
// CompareInfo instances:
//    a CompareInfo instance associated with the "Spanish - Spain" 
// culture with international sort,
//    a CompareInfo instance associated with the "Spanish - Spain" 
// culture with traditional sort, and
//    a CompareInfo instance associated with the InvariantCulture.

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, myStr2);
        Console.WriteLine("   With myCompIntl.Compare: {0}",
            System.Convert.ToString( myCompIntl.Compare(myStr1, myStr2)));
        Console.WriteLine("   With myCompTrad.Compare: {0}", 
            System.Convert.ToString(myCompTrad.Compare(myStr1, myStr2)));
        Console.WriteLine("   With myCompInva.Compare: {0}", 
            System.Convert.ToString(myCompInva.Compare(myStr1, myStr2)));
    } //main 
} //SamplesCompareInfo

/*
This code produces the following output.

Comparing "calle" and "calor"
   With myCompIntl.Compare: -1
   With myCompTrad.Compare: 1
   With myCompInva.Compare: -1
*/

The following code example demonstrates calling the Compare method.

using System;
using System.Text;
using System.Globalization;

public sealed class App
{
    static void Main(string[] args)
    {
        String[] sign = new String[] { "<", "=", ">" };

        // The code below demonstrates how strings compare 
        // differently for different cultures.
        String s1 = "Coté", s2 = "coté", s3 = "côte";

        // Set sort order of strings for French in France.
        CompareInfo ci = new CultureInfo("fr-FR").CompareInfo;
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID);
      
        // Display the result using fr-FR Compare of Coté = coté.  	
        Console.WriteLine("fr-FR Compare: {0} {2} {1}",
            s1, s2, sign[ci.Compare(s1, s2, CompareOptions.IgnoreCase) + 1]);

        // Display the result using fr-FR Compare of coté > côte.
        Console.WriteLine("fr-FR Compare: {0} {2} {1}",
            s2, s3, sign[ci.Compare(s2, s3, CompareOptions.None) + 1]);

        // Set sort order of strings for Japanese as spoken in Japan.
        ci = new CultureInfo("ja-JP").CompareInfo;
        Console.WriteLine("The LCID for {0} is {1}.", ci.Name, ci.LCID);

        // Display the result using ja-JP Compare of coté < côte. 
        Console.WriteLine("ja-JP Compare: {0} {2} {1}",
            s2, s3, sign[ci.Compare(s2, s3) + 1]);
    }
}

// This code produces the following output.
// 
// The LCID for fr-FR is 1036.
// fr-FR Compare: Coté = coté
// fr-FR Compare: coté > côte
// The LCID for ja-JP is 1041.
// ja-JP Compare: coté < côte

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