String.Compare Method (String, String)



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

static member Compare : 
        strA:string *
        strB:string -> int


Type: System.String

The first string to compare.

Type: System.String

The second string to compare.

Return Value

Type: System.Int32




Less than zero

strA precedes strB in the sort order.



Greater than zero

strA follows strB in the sort order.




When comparing strings, you should call theCompare(String, String, StringComparison) method, which requires that you explicitly specify the type of string comparison that the method uses. For more information, see Best Practices for Using Strings in the .NET Framework.







Notes to Callers:

Character sets include ignorable characters. The Compare(String, String) method does not consider such characters when it performs a culture-sensitive comparison. For example, if the following code is run on the .NET Framework 4 or later, a culture-sensitive comparison of "animal" with "ani-mal" (using a soft hyphen, or U+00AD) indicates that the two strings are equivalent.


To recognize ignorable characters in a string comparison, call the Compare(String, String, StringComparison) method and supply a value of either CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for the comparisonType parameter.

The following example calls the Compare(String, String) method to compare three sets of strings.


In the following example, the ReverseStringComparer class demonstrates how you can evaluate two strings with the Compare method.


Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top