Export (0) Print
Expand All

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

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

Community Additions

ADD
Show:
© 2014 Microsoft