Mono Class Library: System.String Overview | Members

System.String.Compare Method

Compares substrings of two strings. [Edit]

public static int Compare (string strA, int indexA, string strB, int indexB, int length)

Parameters

strA
The first string to compare. Can be a null reference. [Edit]
indexA
A int containing the starting index of the substring within strA. [Edit]
strB
The second string to compare. Can be a null reference. [Edit]
indexB
A int containing the starting index of the substring within strB. [Edit]
length
A int containing the maximum number of characters in the substrings to compare. If length is zero, then zero is returned. [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 numberThe substring in strA is < the substring in strB.
ZeroThe substring in strA == the substring in strB, or length is zero.
A positive numberThe substring in strA is > the substring in strB.
[Edit]

Exceptions

TypeReason
ArgumentOutOfRangeException

The sum of indexA and length is greater than strA .Length .

-or-

The sum of indexB and length is greater than strB .Length .

-or-

indexA, indexB, or length is negative.

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

[Edit]

Example

The following example demonstrates comparing substrings.

C# Example
using System;
public class StringCompareExample {
 public static void Main() {
 string strA = "A string";
 string strB = "B ring";
 int first = String.Compare( strA, 4, strB, 2, 3 );
 int second = String.Compare( strA, 3, strB, 3, 3 );
 Console.WriteLine( "When the substring 'rin' of 'A string' is compared to the substring 'rin' of 'B ring', the return value is {0}.", first );
 Console.WriteLine( "When the substring 'tri' of 'A string' is compared to the substring 'ing' of 'B ring', the return value is {0}.", second );
 }
}
   

The output is

When the substring 'rin' of 'A string' is compared to the substring 'rin' of 'B ring', the return value is 0.
When the substring 'tri' of 'A string' is compared to the substring 'ing' of 'B ring', the return value is 1.

Requirements

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