다음을 통해 공유


String.CompareOrdinal Method (String, Int32, String, Int32, Int32)

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

Compares substrings of two specified String objects by evaluating the numeric values of the corresponding Char objects in each substring.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function CompareOrdinal ( _
    strA As String, _
    indexA As Integer, _
    strB As String, _
    indexB As Integer, _
    length As Integer _
) As Integer
[SecuritySafeCriticalAttribute]
public static int CompareOrdinal(
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length
)

Parameters

  • strA
    Type: System.String
    The first string to use in the comparison.
  • indexA
    Type: System.Int32
    The starting index of the substring in strA.
  • strB
    Type: System.String
    The second string to use in the comparison.
  • indexB
    Type: System.Int32
    The starting index of the substring in strB.
  • length
    Type: System.Int32
    The maximum number of characters in the substrings to compare.

Return Value

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

Value

Condition

Less than zero

The substring in strA is less than the substring in strB.

Zero

The substrings are equal, or length is zero.

Greater than zero

The substring in strA is greater than the substring in strB.

Exceptions

Exception Condition
ArgumentOutOfRangeException

strA is not nulla null reference (Nothing in Visual Basic) and indexA is greater than strA. Length.

-or-

strB is not nulla null reference (Nothing in Visual Basic) and indexB is greater than strB. Length.

-or-

indexA, indexB, or length is negative.

Remarks

The indexA, indexB, and length parameters must be nonnegative.

The number of characters compared is the lesser of the length of strA less indexA, the length of strB less indexB, and length.

This method performs a case-sensitive comparison using ordinal sort rules. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

Because CompareOrdinal is a static method, strA and strB can be nulla null reference (Nothing in Visual Basic). If both values are nulla null reference (Nothing in Visual Basic), the method returns 0 (zero), which indicates that strA and strB are equal. If only one of the values is nulla null reference (Nothing in Visual Basic), the method considers the non-null value to be greater.

Examples

This following code example demonstrates that CompareOrdinal and Compare use different sort orders.

Imports System.Globalization

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim strLow As [String] = "abc"
      Dim strCap As [String] = "ABC"
      Dim result As [String] = "equal to "
      Dim x As Integer = 0
      Dim pos As Integer = 1

      ' The Unicode codepoint for 'b' is greater than the codepoint for 'B'.      
      x = String.CompareOrdinal(strLow, pos, strCap, pos, 1)
      If x < 0 Then
         result = "less than"
      End If
      If x > 0 Then
         result = "greater than"
      End If

      ' In U.S. English culture, 'b' is linguistically less than 'B'.
      outputBlock.Text += String.Format("CompareOrdinal(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos) & vbCrLf

      outputBlock.Text += String.Format("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos)) & vbCrLf

      x = String.Compare(strLow, pos, strCap, pos, 1, New CultureInfo("en-US"), CompareOptions.None)
      If x < 0 Then
         result = "less than"
      ElseIf x > 0 Then
         result = "greater than"
      End If
      outputBlock.Text += String.Format("Compare(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos) & vbCrLf
      outputBlock.Text += String.Format("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos)) & vbCrLf
   End Sub 'Main
End Class 'Test
using System;
using System.Globalization;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      String strLow = "abc";
      String strCap = "ABC";
      String result = "equal to ";
      int x = 0;
      int pos = 1;

      // The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
      x = String.CompareOrdinal(strLow, pos, strCap, pos, 1);
      if (x < 0) result = "less than";
      if (x > 0) result = "greater than";
      outputBlock.Text += String.Format("CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos) + "\n";
      outputBlock.Text += String.Format("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]) + "\n";

      // In U.S. English culture, 'b' is linguistically less than 'B'.
      x = String.Compare(strLow, pos, strCap, pos, 1, new CultureInfo("en-US"), CompareOptions.None);
      if (x < 0) result = "less than";
      else if (x > 0) result = "greater than";
      outputBlock.Text += String.Format("Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos) + "\n";
      outputBlock.Text += String.Format("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]) + "\n";
   }
}

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.