C# String Compare(String, Int32, String, Int32, Int32, StringComparison)
Description
String Compare(String, Int32, String, Int32, Int32, StringComparison)
compares substrings of two specified String objects using the specified
rules, and returns an integer that indicates their relative position in
the sort order.
Syntax
String.Compare(String, Int32, String, Int32, Int32, StringComparison)
has the following syntax.
public static int Compare(
string strA,//www. ja va2 s . co m
int indexA,
string strB,
int indexB,
int length,
StringComparison comparisonType
)
Parameters
String.Compare(String, Int32, String, Int32, Int32, StringComparison)
has the following parameters.
strA
- The first string to use in the comparison.indexA
- The position of the substring within strA.strB
- The second string to use in the comparison.indexB
- The position of the substring within strB.length
- The maximum number of characters in the substrings to compare.comparisonType
- One of the enumeration values that specifies the rules to use in the comparison.
Returns
String.Compare(String, Int32, String, Int32, Int32, StringComparison)
method returns A 32-bit signed integer that indicates the lexical relationship between
the two comparands. Value Condition Less than zero The substring in the strA
parameter is less than the substring in the strB parameter. Zero The substrings
are equal, or the length parameter is zero. Greater than zero The substring
in strA is greater than the substring in strB.
Example
The following example compares two substrings. Sample for String.Compare(String, Int32, String, Int32, Int32)
using System;//from w w w . j ava 2 s .c o m
class Sample {
public static void Main() {
// 0123456
String str1 = "cSharp Demo";
String str2 = "cSharp demo";
String str;
int result;
result = String.Compare(str1, 2, str2, 0, 2);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
}
}
The code above generates the following result.