C# String CompareOrdinal(String, Int32, String, Int32, Int32)
Description
String CompareOrdinal(String, Int32, String, Int32, Int32)
compares substrings of two specified String objects by evaluating
the numeric values of the corresponding Char objects in each substring.
Syntax
String.CompareOrdinal(String, Int32, String, Int32, Int32)
has the following syntax.
public static int CompareOrdinal(
string strA,//from w w w. ja v a 2s.co m
int indexA,
string strB,
int indexB,
int length
)
Parameters
String.CompareOrdinal(String, Int32, String, Int32, Int32)
has the following parameters.
strA
- The first string to use in the comparison.indexA
- The starting index of the substring in strA.strB
- The second string to use in the comparison.indexB
- The starting index of the substring in strB.length
- The maximum number of characters in the substrings to compare.
Returns
String.CompareOrdinal(String, Int32, String, Int32, Int32)
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
This following example demonstrates that CompareOrdinal and Compare use different sort orders.
// w ww .jav a 2 s. c o m
using System;
using System.Globalization;
class Test
{
public static void Main(String[] args)
{
String strLow = "abc";
String strCap = "ABC";
String result = "equal to ";
int x = 0;
int pos = 1;
x = String.CompareOrdinal(strLow, pos, strCap, pos, 1);
if (x < 0) result = "less than";
if (x > 0) result = "greater than";
Console.WriteLine(result);
x = String.Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo("en-US"));
if (x < 0) result = "less than";
else if (x > 0) result = "greater than";
Console.WriteLine(result);
}
}
The code above generates the following result.