Export (0) Print
Expand All

String.CompareOrdinal Method

Compares two String objects by evaluating the numeric values of the corresponding Char objects in each string.

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function CompareOrdinal(String, String) As Integer
[C#] public static int CompareOrdinal(string, string);
[C++] public: static int CompareOrdinal(String*, String*);
[JScript] public static function CompareOrdinal(String, String) : int;

Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each substring. Parameters specify the length and starting positions of the substrings.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function CompareOrdinal(String, Integer, String, Integer, Integer) As Integer
[C#] public static int CompareOrdinal(string, int, string, int, int);
[C++] public: static int CompareOrdinal(String*, int, String*, int, int);
[JScript] public static function CompareOrdinal(String, int, String, int, int) : int;

Example

[Visual Basic, C#, C++] This following example demonstrates that CompareOrdinal and Compare use different sort orders.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of CompareOrdinal. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Globalization

Class Test
   
  Public Shared Sub Main(args() As [String])
      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'.
      Console.WriteLine("CompareOrdinal(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
      
      Console.WriteLine("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
      
      x = [String].Compare(strLow, pos, strCap, pos, 1, False, New CultureInfo("en-US"))
      If x < 0 Then
         result = "less than"
      ElseIf x > 0 Then
         result = "greater than"
      End If
      Console.WriteLine("Compare(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
      Console.WriteLine("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
   End Sub 'Main
End Class 'Test

[C#] 
using System;
using System.Globalization;

class Test 
{
    public static void Main(String[] args) 
    {
    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";
    Console.WriteLine("CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
    Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);

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

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Globalization;

int main()
{
   String* strLow = S"abc";
   String* strCap = S"ABC";
   String* result = S"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 = S"less than";
   if (x > 0) result = S"greater than";
   Console::WriteLine(S"CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, __box(pos));
   Console::WriteLine(S"   '{0}' is {1} '{2}'", __box(strLow->Chars[pos]), result, __box(strCap->Chars[pos]));

   // In U.S. English culture, 'b' is linguistically less than 'B'.
   x = String::Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo(S"en-US"));
   if (x < 0) result = S"less than";
   else if (x > 0) result = S"greater than";
   Console::WriteLine(S"Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, __box(pos));
   Console::WriteLine(S"   '{0}' is {1} '{2}'", __box(strLow->Chars[pos]), result, __box(strCap->Chars[pos]));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

String Class | String Members | System Namespace

Show:
© 2014 Microsoft