CompareInfo.Compare Method (String, String)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Compares two strings.

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

abstract Compare : 
        string1:string *
        string2:string -> int
override Compare : 
        string1:string *
        string2:string -> int

Parameters

string1
Type: System.String

The first string to compare.

string2
Type: System.String

The second string to compare.

Return Value

Type: System.Int32

A 32-bit signed integer indicating the lexical relationship between the two comparands.

Value

Condition

zero

The two strings are equal.

less than zero

string1 is less than string2.

greater than zero

string1 is greater than string2.

By default, the comparison is performed by using CompareOptions.None. If a security decision depends on a string comparison or a case change, you should use the InvariantCulture property to ensure that the behavior is consistent regardless of the culture settings of the operating system.

System_CAPS_noteNote

When possible, you should call string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify Ordinal or OrdinalIgnoreCase for security comparisons.

Notes to Callers:

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. The Compare(String, String) method does not consider such characters when it performs a culture-sensitive comparison. For instance, a culture-sensitive comparison of "animal" with "ani-mal" (using a soft hyphen, or U+00AD) indicates that the two strings are equivalent, as the following example shows.

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim ci As CompareInfo = CultureInfo.CurrentCulture.CompareInfo
      Dim s1 As String = "ani" + ChrW(&h00AD) + "mal"
      Dim s2 As String = "animal"

      Console.WriteLine("Comparison of '{0}' and '{1}': {2}", 
                        s1, s2, ci.Compare(s1, s2))
  End Sub
End Module
' The example displays the following output:
'       Comparison of 'ani-mal' and 'animal': 0

To recognize ignorable characters in a string comparison, call the Compare(String, String, CompareOptions) method and supply a value of either CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for the options parameter.

The following example compares portions of two strings using the different CompareInfo objects:

No code example is currently available or this language may not be supported.

The following example demonstrates calling the Compare method.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: