Bagikan melalui


String.Compare Method (String, String, StringComparison)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Compares two specified String objects using the specified string comparison options and returns an integer that indicates their relationship to one another in the sort order.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function Compare ( _
    strA As String, _
    strB As String, _
    comparisonType As StringComparison _
) As Integer
[SecuritySafeCriticalAttribute]
public static int Compare(
    string strA,
    string strB,
    StringComparison comparisonType
)

Parameters

  • comparisonType
    Type: System.StringComparison
    One of the enumeration values that specifies the rules to use in the comparison.

Return Value

Type: System.Int32
A 32-bit signed integer indicating the lexical relationship between the two comparands.

Value

Condition

Less than zero

strA is less than strB.

Zero

strA equals strB.

Greater than zero

strA is greater than strB.

Exceptions

Exception Condition
ArgumentException

comparisonType is not a StringComparison value.

NotSupportedException

StringComparison is not supported.

Remarks

The comparisonType parameter indicates whether the comparison should use the current or invariant culture, honor or ignore the case of the comparands, or use word (culture-sensitive) or ordinal (culture-insensitive) sort rules.

One or both comparands can be nulla null reference (Nothing in Visual Basic). By definition, any string, including the empty string (""), compares greater than a null reference; and two null references compare equal to each other.

The comparison terminates when an inequality is discovered or both strings have been compared. However, if the two strings compare equal to the end of one string, and the other string has characters remaining, the string with remaining characters is considered greater. The return value is the result of the last comparison performed.

Unexpected results can occur when comparisons are affected by culture-specific casing rules. For example, in Turkish, the following example yields the wrong results because the file system in Turkish does not use linguistic casing rules for the letter 'i' in "file".

Compare the path name to "file" using an ordinal comparison. The correct code to do this is as follows:

Examples

The following code example compares three versions of the letter 'I'. The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed.

' This example demonstrates the 
' System.String.Compare(String, String, StringComparison) method.

Imports System.Threading

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim intro As String = "Compare three versions of the letter I using different " & _
                            "values of StringComparison."

      ' Define an array of strings where each element contains a version of the 
      ' letter I. (An array of strings is used so you can easily modify this 
      ' code example to test additional or different combinations of strings.)  
      Dim threeIs(2) As String
      ' LATIN SMALL LETTER I (U+0069)
      threeIs(0) = "i"
      ' LATIN SMALL LETTER DOTLESS I (U+0131)
      threeIs(1) = "ı"
      ' LATIN CAPITAL LETTER I (U+0049)
      threeIs(2) = "I"

      Dim unicodeNames As String() = { _
                          "LATIN SMALL LETTER I (U+0069)", _
                          "LATIN SMALL LETTER DOTLESS I (U+0131)", _
                          "LATIN CAPITAL LETTER I (U+0049)"}

      Dim scValues As StringComparison() = { _
                          StringComparison.CurrentCulture, _
                          StringComparison.CurrentCultureIgnoreCase, _
                          StringComparison.InvariantCulture, _
                          StringComparison.InvariantCultureIgnoreCase, _
                          StringComparison.Ordinal, _
                          StringComparison.OrdinalIgnoreCase}
      '
      outputBlock.Text &= intro & vbCrLf

      ' Display the current culture because the culture-specific comparisons
      ' can produce different results with different cultures.
      outputBlock.Text &= String.Format("The current culture is {0}." & vbCrLf, _
                         Thread.CurrentThread.CurrentCulture.Name) & vbCrLf

      ' Determine the relative sort order of three versions of the letter I. 
      Dim sc As StringComparison
      For Each sc In scValues
         outputBlock.Text &= String.Format("StringComparison.{0}:", sc) & vbCrLf

         ' LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131)
         Test(outputBlock, 0, 1, sc, threeIs, unicodeNames)

         ' LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049)
         Test(outputBlock, 0, 2, sc, threeIs, unicodeNames)

         ' LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049)
         Test(outputBlock, 1, 2, sc, threeIs, unicodeNames)

         outputBlock.Text &= vbCrLf
      Next sc

   End Sub 'Main

   Protected Shared Sub Test(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal x As Integer, ByVal y As Integer, _
                             ByVal comparison As StringComparison, _
                             ByVal testI() As String, ByVal testNames() As String)
      Dim resultFmt As String = "{0} is {1} {2}"
      Dim result As String = "equal to"
      Dim cmpValue As Integer = 0
      '
      cmpValue = String.Compare(testI(x), testI(y), comparison)
      If cmpValue < 0 Then
         result = "less than"
      ElseIf cmpValue > 0 Then
         result = "greater than"
      End If
      outputBlock.Text &= String.Format(resultFmt, testNames(x), result, testNames(y)) & vbCrLf

   End Sub 'Test
End Class 'Sample

'
'This code example produces the following results:
'
'Compare three versions of the letter I using different values of StringComparison.
'The current culture is en-US.
'
'StringComparison.CurrentCulture:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)
'
'StringComparison.CurrentCultureIgnoreCase:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)
'
'StringComparison.InvariantCulture:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)
'
'StringComparison.InvariantCultureIgnoreCase:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)
'
'StringComparison.Ordinal:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)
'
'StringComparison.OrdinalIgnoreCase:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)
'
// This example demonstrates the 
// System.String.Compare(String, String, StringComparison) method.

using System;
using System.Threading;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string intro = "Compare three versions of the letter I using different " +
                     "values of StringComparison.";

      // Define an array of strings where each element contains a version of the 
      // letter I. (An array of strings is used so you can easily modify this 
      // code example to test additional or different combinations of strings.)  

      string[] threeIs = new string[3];
      // LATIN SMALL LETTER I (U+0069)
      threeIs[0] = "\u0069";
      // LATIN SMALL LETTER DOTLESS I (U+0131)
      threeIs[1] = "\u0131";
      // LATIN CAPITAL LETTER I (U+0049)
      threeIs[2] = "\u0049";

      string[] unicodeNames = 
             {
             "LATIN SMALL LETTER I (U+0069)", 
             "LATIN SMALL LETTER DOTLESS I (U+0131)", 
             "LATIN CAPITAL LETTER I (U+0049)"
             };

      StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

      //
      outputBlock.Text += intro + "\n";

      // Display the current culture because the culture-specific comparisons
      // can produce different results with different cultures.
      outputBlock.Text += String.Format("The current culture is {0}.\n",
                         Thread.CurrentThread.CurrentCulture.Name) + "\n";

      // Determine the relative sort order of three versions of the letter I. 
      foreach (StringComparison sc in scValues)
      {
         outputBlock.Text += String.Format("StringComparison.{0}:", sc) + "\n";

         // LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131)
         Test(outputBlock, 0, 1, sc, threeIs, unicodeNames);

         // LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049)
         Test(outputBlock, 0, 2, sc, threeIs, unicodeNames);

         // LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049)
         Test(outputBlock, 1, 2, sc, threeIs, unicodeNames);

         outputBlock.Text += "\n";
      }
   }

   protected static void Test(System.Windows.Controls.TextBlock outputBlock, int x, int y,
                              StringComparison comparison,
                              string[] testI, string[] testNames)
   {
      string resultFmt = "{0} is {1} {2}";
      string result = "equal to";
      int cmpValue = 0;
      //
      cmpValue = String.Compare(testI[x], testI[y], comparison);
      if (cmpValue < 0)
         result = "less than";
      else if (cmpValue > 0)
         result = "greater than";
      outputBlock.Text += String.Format(resultFmt, testNames[x], result, testNames[y]) + "\n";
   }
}

/*
This code example produces the following results:

Compare three versions of the letter I using different values of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.CurrentCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.InvariantCulture:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.InvariantCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.Ordinal:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.OrdinalIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

*/

Version Information

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

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.