This documentation is archived and is not being maintained.

CompareInfo.Compare Method (String, String)

Compares two strings.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

abstract Compare : 
        string1:string * 
        string2:string -> int 
override Compare : 
        string1:string * 
        string2:string -> int 

Parameters

string1
Type: System.String
The first string to compare.
string2
Type: System.String
The second string to compare.

Return Value

Type: System.Int32

Value

Condition

zero

The two strings are equal.

less than zero

string1 is less than string2.

greater than zero

string1 is greater than string2.

The comparison is performed, by default, using None. If a security decision depends on a string comparison or a case change, the application should use the InvariantCulture to ensure that the behavior is consistent regardless of the culture settings of the operating system.

NoteNote

When possible, the application should use string comparison methods that accept a CompareOptions value to specify the kind of comparison expected. As a general rule, user-facing comparisons are best served by the use of linguistic options (using the current culture), while security comparisons should specify Ordinal or OrdinalIgnoreCase.

The following code example compares portions of two strings using the different CompareInfo objects:

No code example is currently available or this language may not be supported.

The following code example demonstrates calling the Compare method.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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