Mono Class Library: System.String Overview | Members

System.String.Compare Method

Returns sort order of two string objects, ignoring or honoring their case. [Edit]

public static int Compare (string strA, string strB, bool ignoreCase)

Parameters

strA
The first string to compare. Can be a null reference. [Edit]
strB
The second string to compare. Can be a null reference. [Edit]
ignoreCase
A bool indicating whether the comparison is case-insensitive. If ignoreCase is true, the comparison is case-insensitive. If ignoreCase is false, the comparison is case-sensitive, and uppercase letters evaluate greater than their lowercase equivalents. [Edit]

Returns

The return value is a negative number, zero, or a positive number reflecting the sort order of the specified substrings. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:

ValueMeaning
A negative numberstrA is < strB.
ZerostrA == strB.
A positive numberstrA is > strB.
[Edit]

Remarks

Note:

The result of comparing any string (including the empty string) to a null reference is greater than zero. The result of comparing two null references is zero. Uppercase letters evaluate greater than their lowercase equivalents.

The method uses the culture (if any) of the current thread to determine the ordering of individual characters. The two strings are compared on a character-by-character basis.

String.Compare(strA, strB, false) is equivalent to String.Compare(strA, strB ).

[Edit]

Example

The following example demonstrates comparing strings with and without case sensitivity.

C# Example
using System;
public class StringCompareExample {
 public static void Main() {
 string strA = "A STRING";
 string strB = "a string";
 int first = String.Compare( strA, strB, true );
 int second = String.Compare( strA, strB, false );
 Console.WriteLine( "When 'A STRING' is compared to 'a string' in a case-insensitive manner, the return value is {0}.", first );
 Console.WriteLine( "When 'A STRING' is compared to 'a string' in a case-sensitive manner, the return value is {0}.", second );
 }
}
   

The output is

When 'A STRING' is compared to 'a string' in a case-insensitive manner, the return value is 0.
When 'A STRING' is compared to 'a string' in a case-sensitive manner, the return value is 1.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0