Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.CompareOrdinal Method (String, String)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Shared Function CompareOrdinal ( _
	strA As String, _
	strB As String _
) As Integer

Parameters

strA
Type: System.String

The first string to compare.

strB
Type: System.String

The second string to compare.

Return Value

Type: System.Int32
An integer that indicates 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.

This method performs a case-sensitive comparison using ordinal sort rules. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions. To perform a case-insensitive comparison using ordinal sort rules, call the Compare(String, String, StringComparison) method with a StringComparison value of OrdinalIgnoreCase.

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

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

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft. All rights reserved.