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.
2 out of 2 rated this helpful - Rate this topic

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

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)
'Declaration
Public Shared Function CompareOrdinal ( _
	strA As String, _
	indexA As Integer, _
	strB As String, _
	indexB As Integer, _
	length As Integer _
) As Integer

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 that indicates 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.

ExceptionCondition
ArgumentOutOfRangeException

strA is not Nothing and indexA is greater than strA.Length.

-or-

strB is not Nothing andindexB is greater than strB.Length.

-or-

indexA, indexB, or length is negative.

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. To perform a case-insensitive comparison using ordinal sort rules, call the Compare(String, Int32, String, Int32, Int32, 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.

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

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

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.