Partager via


String.CompareOrdinal Method (String, String)

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

Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string and returns an integer that indicates their relative position in the sort order.

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

Syntax

'Declaration
Public Shared Function CompareOrdinal ( _
    strA As String, _
    strB As String _
) As Integer
public static int CompareOrdinal(
    string strA,
    string strB
)

Parameters

Return Value

Type: System.Int32
An integer indicating the lexical relationship between the two comparands.

Value

Condition

Less than zero

strA is less than strB.

Zero

strA and strB are equal.

Greater than zero

strA is greater than strB.

Remarks

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

The following code example performs and ordinal comparison of two strings that only differ in case.

' Sample for String.CompareOrdinal(String, String)

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim str1 As [String] = "ABCD"
      Dim str2 As [String] = "abcd"
      Dim str As [String]
      Dim result As Integer

      outputBlock.Text &= vbCrLf
      outputBlock.Text &= "Compare the numeric values of the corresponding Char objects in each string." & vbCrLf
      outputBlock.Text += String.Format("str1 = '{0}', str2 = '{1}'", str1, str2) & vbCrLf
      result = [String].CompareOrdinal(str1, str2)
      str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
      outputBlock.Text += String.Format("String '{0}' is ", str1)
      outputBlock.Text += String.Format("{0} ", str)
      outputBlock.Text += String.Format("String '{0}'.", str2) & vbCrLf
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'Compare the numeric values of the corresponding Char objects in each string.
'str1 = 'ABCD', str2 = 'abcd'
'String 'ABCD' is less than String 'abcd'.
'
// Sample for String.CompareOrdinal(String, String)
using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      String str1 = "ABCD";
      String str2 = "abcd";
      String str;
      int result;

      outputBlock.Text += "\n";
      outputBlock.Text += "Compare the numeric values of the corresponding Char objects in each string." + "\n";
      outputBlock.Text += String.Format("str1 = '{0}', str2 = '{1}'", str1, str2) + "\n";
      result = String.CompareOrdinal(str1, str2);
      str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
      outputBlock.Text += String.Format("String '{0}' is ", str1);
      outputBlock.Text += String.Format("{0} ", str);
      outputBlock.Text += String.Format("String '{0}'.", str2) + "\n";
   }
}
/*
This example produces the following results:

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*/

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.