Export (0) Print
Expand All

String.CompareOrdinal Method (String, String)

.NET Framework 1.1

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

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

Parameters

strA
The first String.
strB
The second String.

Return Value

An integer indicating the lexical relationship between the two comparands.

Permission Description
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 comparison using ordinal sort rules. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

Example

[Visual Basic] 
' Sample for String.CompareOrdinal(String, String)
Imports System
Imports Microsoft.VisualBasic

Class Sample
   Public Shared Sub Main()
      Dim str1 As [String] = "ABCD"
      Dim str2 As [String] = "abcd"
      Dim str As [String]
      Dim result As Integer
      
      Console.WriteLine()
      Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.")
      Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
      result = [String].CompareOrdinal(str1, str2)
      str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
      Console.Write("String '{0}' is ", str1)
      Console.Write("{0} ", str)
      Console.WriteLine("String '{0}'.", str2)
   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'.
'

[C#] 
// Sample for String.CompareOrdinal(String, String)
using System;

class Sample {
    public static void Main() {
    String str1 = "ABCD";
    String str2 = "abcd";
    String str;
    int result;

    Console.WriteLine();
    Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.");
    Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
    result = String.CompareOrdinal(str1, str2);
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("String '{0}' is ", str1);
    Console.Write("{0} ", str);
    Console.WriteLine("String '{0}'.", str2);
    }
}
/*
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'.
*/

[C++] 
// Sample for String::CompareOrdinal(String, String)
#using <mscorlib.dll>

using namespace System;

int main() {
   String*  str1 = S"ABCD";
   String*  str2 = S"abcd";
   String* str;
   int result;

   Console::WriteLine();
   Console::WriteLine(S"Compare the numeric values of the corresponding Char objects in each string.");
   Console::WriteLine(S"str1 = '{0}', str2 = '{1}'", str1, str2);
   result = String::CompareOrdinal(str1, str2);
   str = ((result < 0) ? S"less than" : ((result > 0) ? S"greater than" : S"equal to"));
   Console::Write(S"String '{0}' is ", str1);
   Console::Write(S"{0} ", str);
   Console::WriteLine(S"String '{0}'.", str2);
}
/*
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'.
*/

[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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String.CompareOrdinal Overload List | Int32 | Compare | CompareTo

Show:
© 2014 Microsoft