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, String, CompareOptions)

Compares two strings using the specified CompareOptions value.

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

'Declaration
Public Overridable Function Compare ( _
	string1 As String, _
	string2 As String, _
	options As CompareOptions _
) As Integer
'Usage
Dim instance As CompareInfo
Dim string1 As String
Dim string2 As String
Dim options As CompareOptions
Dim returnValue As Integer

returnValue = instance.Compare(string1, string2, options)
public int Compare (
	String string1, 
	String string2, 
	CompareOptions options
)
public function Compare (
	string1 : String, 
	string2 : String, 
	options : CompareOptions
) : int

Parameters

string1

The first string to compare.

string2

The second string to compare.

options

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

Value

Condition

zero

The two strings are equal.

less than zero

string1 is less than string2.

greater than zero

string1 is greater than string2.

Exception typeCondition

ArgumentException

options contains an invalid CompareOptions value.

If a security decision depends on a string comparison or a case-change operation, use the InvariantCulture to ensure that the behavior will be consistent regardless of the culture settings of the system.

The following code example compares two strings using different CompareOptions settings.

Imports System
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "My Uncle Bill's clients"
      Dim myStr2 As [String] = "My uncle bills clients"

      ' Creates a CompareInfo that uses the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myComp.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1, myStr2)
      Console.WriteLine("   With no CompareOptions            : {0}", myComp.Compare(myStr1, myStr2))
      Console.WriteLine("   With None                         : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.None))
      Console.WriteLine("   With Ordinal                      : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.Ordinal))
      Console.WriteLine("   With StringSort                   : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.StringSort))
      Console.WriteLine("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreCase))
      Console.WriteLine("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreSymbols))
      Console.WriteLine("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, myStr2, CompareOptions.IgnoreCase Or CompareOptions.IgnoreSymbols))

   End Sub 'Main 

End Class 'SamplesCompareInfo


'This code produces the following output.
'
'Comparing "My Uncle Bill's clients" and "My uncle bills clients"
'   With no CompareOptions            : 1
'   With None                         : 1
'   With Ordinal                      : -32
'   With StringSort                   : -1
'   With IgnoreCase                   : 1
'   With IgnoreSymbols                : 1
'   With IgnoreCase and IgnoreSymbols : 0


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

public class SamplesCompareInfo
{
    public static void main(String[] args)
    {
        // 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.get_InvariantCulture().get_CompareInfo();

        // Compares two strings using myComp.
        Console.WriteLine("Comparing \"{0}\" and \"{1}\"", myStr1, myStr2);
        Console.WriteLine("   With no CompareOptions            : {0}",
            System.Convert.ToString( myComp.Compare(myStr1, myStr2)));
        Console.WriteLine("   With None                         : {0}", 
            System.Convert.ToString(myComp.Compare(myStr1, myStr2, 
            CompareOptions.None)));
        Console.WriteLine("   With Ordinal                      : {0}", 
            System.Convert.ToString(myComp.Compare(myStr1, myStr2, 
            CompareOptions.Ordinal)));
        Console.WriteLine("   With StringSort                   : {0}", 
            System.Convert.ToString(myComp.Compare(myStr1, myStr2, 
            CompareOptions.StringSort)));
        Console.WriteLine("   With IgnoreCase                   : {0}", 
            System.Convert.ToString(myComp.Compare(myStr1, myStr2, 
            CompareOptions.IgnoreCase)));
        Console.WriteLine("   With IgnoreSymbols                : {0}", 
            System.Convert.ToString(myComp.Compare(myStr1, myStr2, 
            CompareOptions.IgnoreSymbols)));
        Console.WriteLine("   With IgnoreCase and IgnoreSymbols : {0}", 
            System.Convert.ToString(myComp.Compare(myStr1, myStr2, 
            CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols)));
    } //main 
} //SamplesCompareInfo

/*
This code produces the following output.

Comparing "My Uncle Bill's clients" and "My uncle bills clients"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : -1
   With IgnoreCase                   : 1
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0
*/

The following code example demonstrates calling the Compare method.

Imports System
Imports System.Text
Imports System.Globalization

NotInheritable Public Class App
    Shared Sub Main(ByVal args() As String) 
        Dim sign() As String = {"<", "=", ">"}
        
        ' The code below demonstrates how strings compare 
        ' differently for different cultures.
        Dim s1 As String = "Coté"
        Dim s2 As String = "coté"
        Dim s3 As String = "côte"
        
        ' Set sort order of strings for French in France.
        Dim ci As CompareInfo = 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)))
    End Sub 'Main
End Class 'App

' 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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft