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, 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)

public static int CompareOrdinal (
	string strA,
	int indexA,
	string strB,
	int indexB,
	int length
)
public static int CompareOrdinal (
	String strA, 
	int indexA, 
	String strB, 
	int indexB, 
	int length
)
public static function CompareOrdinal (
	strA : String, 
	indexA : int, 
	strB : String, 
	indexB : int, 
	length : int
) : int
Not applicable.

Parameters

strA

The first String.

indexA

The starting index of the substring in strA.

strB

The second String.

indexB

The starting index of the substring in strB.

length

The maximum number of characters in the substrings to compare.

Return Value

A 32-bit signed integer indicating 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.

Exception typeCondition

ArgumentOutOfRangeException

indexA is greater than strA. Length.

-or-

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

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

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]);
	}
}

import System.*;
import 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, (Int32)pos);
        Console.WriteLine("   '{0}' is {1} '{2}'", 
            (System.Char)strLow.charAt(pos), result, 
            (System.Char)strCap.charAt(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, (Int32)pos);
        Console.WriteLine("   '{0}' is {1} '{2}'", 
            (System.Char)strLow.charAt(pos), result, 
            (System.Char)strCap.charAt(pos));
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft