C# String Compare(String, Int32, String, Int32, Int32, Boolean)
Description
String Compare(String, Int32, String, Int32, Int32, Boolean)
compares substrings of two specified String objects, ignoring
or honoring their case, and returns an integer that indicates their relative
position in the sort order.
Syntax
String.Compare(String, Int32, String, Int32, Int32, Boolean)
has the following syntax.
public static int Compare(
string strA,/* w w w . j a v a 2 s. c om*/
int indexA,
string strB,
int indexB,
int length,
bool ignoreCase
)
Parameters
String.Compare(String, Int32, String, Int32, Int32, Boolean)
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.ignoreCase
- true to ignore case during the comparison; otherwise, false.
Returns
String.Compare(String, Int32, String, Int32, Int32, Boolean)
method returns A 32-bit signed integer that indicates the lexical relationship between
the two comparands. Value Condition Less than zero The substring in strA
is less than the substring in strB. Zero The substrings are equal, or length
is zero. Greater than zero The substring in strA is greater than the substring
in strB.
Example
Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean)
// w w w .j a v a 2 s . co m
using System;
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, 2, 2, true);
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(2, 2), str2);
result = String.Compare(str1, 2, str2, 2, 2, false);
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(2, 2), str2);
}
}
The code above generates the following result.