Mono Class Library: System.String Overview | MembersSystem.String.Compare Method |
Compares substrings of two strings. [Edit]
|
- 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]
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:
[Edit]
Value Meaning A negative number The substring in strA is < the substring in strB. Zero The substring in strA == the substring in strB, or length is zero. A positive number The substring in strA is > the substring in strB.
Type Reason 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]
Note:[Edit]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.
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.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0