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)

public static int CompareOrdinal (
	string strA,
	string strB
)
public static int CompareOrdinal (
	String strA, 
	String strB
)
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.

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.

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

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

// Sample for String.CompareOrdinal(String, String)
import System.*;

class Sample
{
    public static void main(String[] args)
    {
        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);
    } //main
} //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'.
*/

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

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

Community Additions

Show:
© 2014 Microsoft