String.CompareOrdinal 方法

定义

通过计算每个字符串中相应 String 对象的数值来比较两个 Char 对象。Compares two String objects by evaluating the numeric values of the corresponding Char objects in each string.

重载

CompareOrdinal(String, Int32, String, Int32, Int32)

通过计算每个子字符串中相应 String 对象的数值来比较两个指定的 Char 对象的子字符串。Compares substrings of two specified String objects by evaluating the numeric values of the corresponding Char objects in each substring.

CompareOrdinal(String, String)

通过计算每个字符串中相应 String 对象的数值来比较两个指定的 Char 对象。Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string.

CompareOrdinal(String, Int32, String, Int32, Int32)

通过计算每个子字符串中相应 String 对象的数值来比较两个指定的 Char 对象的子字符串。Compares substrings of two specified String objects by evaluating the numeric values of the corresponding Char objects in each substring.

public:
 static int CompareOrdinal(System::String ^ strA, int indexA, System::String ^ strB, int indexB, int length);
public static int CompareOrdinal (string strA, int indexA, string strB, int indexB, int length);
static member CompareOrdinal : string * int * string * int * int -> int
Public Shared Function CompareOrdinal (strA As String, indexA As Integer, strB As String, indexB As Integer, length As Integer) As Integer

参数

strA
String

要在比较中使用的第一个字符串。The first string to use in the comparison.

indexA
Int32

strA 中子字符串的起始索引。The starting index of the substring in strA.

strB
String

要在比较中使用的第二个字符串。The second string to use in the comparison.

indexB
Int32

strB 中子字符串的起始索引。The starting index of the substring in strB.

length
Int32

要比较的子字符串中字符的最大数量。The maximum number of characters in the substrings to compare.

返回

一个 32 位带符号整数,指示两个比较数之间的词法关系。A 32-bit signed integer that indicates the lexical relationship between the two comparands.

Value 条件Condition
小于零Less than zero strA 中的子字符串小于 strB 中的子字符串。The substring in strA is less than the substring in strB.
Zero 子字符串相等,或者 length 为零。The substrings are equal, or length is zero.
大于零Greater than zero strA 中的子字符串大于 strB 中的子字符串。The substring in strA is greater than the substring in strB.

异常

strA 不为 null,且 indexA 大于 strA.LengthstrA is not null and indexA is greater than strA.Length.

-or- strB 不为 null ,且 indexB 大于 strB.LengthstrB is not null and indexB is greater than strB.Length.

-or- indexAindexBlength 为负数。indexA, indexB, or length is negative.

示例

下面的示例演示 CompareOrdinalCompare 使用不同的排序顺序。This following example demonstrates that CompareOrdinal and Compare use different sort orders.

using namespace System;
using namespace System::Globalization;
int main()
{
   String^ strLow = "abc";
   String^ strCap = "ABC";
   String^ result = "equal to ";
   int x = 0;
   int pos = 1;
   
   // The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
   x = String::CompareOrdinal( strLow, pos, strCap, pos, 1 );
   if ( x < 0 )
      result = "less than";

   if ( x > 0 )
      result = "greater than";

   Console::WriteLine( "CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos );
   Console::WriteLine( "   '{0}' is {1} '{2}'", strLow[ pos ], result, strCap[ pos ] );

   // In U.S. English culture, 'b' is linguistically less than 'B'.
   x = String::Compare( strLow, pos, strCap, pos, 1, false, gcnew CultureInfo( "en-US" ) );
   if ( x < 0 )
      result = "less than";
   else
   if ( x > 0 )
      result = "greater than";

   Console::WriteLine( "Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos );
   Console::WriteLine( "   '{0}' is {1} '{2}'", strLow[ pos ], result, strCap[ pos ] );
}
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;

// The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
    x = String.CompareOrdinal(strLow, pos, strCap, pos, 1);
    if (x < 0) result = "less than";
    if (x > 0) result = "greater than";
    Console.WriteLine("CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
    Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);

// In U.S. English culture, 'b' is linguistically less than 'B'.
    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("Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
    Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);
    }
}
Imports System.Globalization

Class Test
   
  Public Shared Sub Main(args() As [String])
      Dim strLow As [String] = "abc"
      Dim strCap As [String] = "ABC"
      Dim result As [String] = "equal to "
      Dim x As Integer = 0
      Dim pos As Integer = 1

' The Unicode codepoint for 'b' is greater than the codepoint for 'B'.      
      x = [String].CompareOrdinal(strLow, pos, strCap, pos, 1)
      If x < 0 Then
         result = "less than"
      End If
      If x > 0 Then
         result = "greater than"
      End If

' In U.S. English culture, 'b' is linguistically less than 'B'.
      Console.WriteLine("CompareOrdinal(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
      
      Console.WriteLine("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
      
      x = [String].Compare(strLow, pos, strCap, pos, 1, False, New CultureInfo("en-US"))
      If x < 0 Then
         result = "less than"
      ElseIf x > 0 Then
         result = "greater than"
      End If
      Console.WriteLine("Compare(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
      Console.WriteLine("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
   End Sub
End Class

注解

indexAindexBlength 参数必须为非负。The indexA, indexB, and length parameters must be nonnegative.

比较的字符数是 strA 的长度越少,indexA的长度越少,strB indexB的长度和 lengthThe number of characters compared is the lesser of the length of strA less indexA, the length of strB less indexB, and length.

此方法使用序号排序规则执行区分大小写的比较。This method performs a case-sensitive comparison using ordinal sort rules. 有关 word、字符串和序号排序的详细信息,请参阅 System.Globalization.CompareOptionsFor more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions. 若要使用序号排序规则执行不区分大小写的比较,请调用 Compare(String, Int32, String, Int32, Int32, StringComparison) 方法,将 comparisonType 参数设置为 StringComparison.OrdinalIgnoreCaseTo perform a case-insensitive comparison using ordinal sort rules, call the Compare(String, Int32, String, Int32, Int32, StringComparison) method with the comparisonType argument set to StringComparison.OrdinalIgnoreCase.

由于 CompareOrdinal(String, String) 是静态方法,因此可以 null``strAstrBBecause CompareOrdinal(String, String) is a static method, strA and strB can be null. 如果同时 null这两个值,则该方法将返回0(零),表示 strAstrB 相等。If both values are null, the method returns 0 (zero), which indicates that strA and strB are equal. 如果只 null其中一个值,则方法会将非 null 值视为更大的值。If only one of the values is null, the method considers the non-null value to be greater.

另请参阅

CompareOrdinal(String, String)

通过计算每个字符串中相应 String 对象的数值来比较两个指定的 Char 对象。Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string.

public:
 static int CompareOrdinal(System::String ^ strA, System::String ^ strB);
public static int CompareOrdinal (string strA, string strB);
static member CompareOrdinal : string * string -> int
Public Shared Function CompareOrdinal (strA As String, strB As String) As Integer

参数

strA
String

要比较的第一个字符串。The first string to compare.

strB
String

要比较的第二个字符串。The second string to compare.

返回

一个整数,指示两个比较字之间的词法关系。An integer that indicates the lexical relationship between the two comparands.

Value 条件Condition
小于零Less than zero strA 小于 strBstrA is less than strB.
Zero strAstrB 相等。strA and strB are equal.
大于零Greater than zero strA 大于 strBstrA is greater than strB.

示例

下面的示例对两个仅大小写不同的字符串执行和序号比较。The following example performs and ordinal comparison of two strings that only differ in case.

// Sample for String::CompareOrdinal(String, String)
using namespace System;
int main()
{
   String^ str1 = "ABCD";
   String^ str2 = "abcd";
   String^ str;
   int result;
   Console::WriteLine();
   Console::WriteLine( "Compare the numeric values of the corresponding Char objects in each string." );
   Console::WriteLine( "str1 = '{0}', str2 = '{1}'", str1, str2 );
   result = String::CompareOrdinal( str1, str2 );
   str = ((result < 0) ? "less than" : ((result > 0) ? (String^)"greater than" : "equal to"));
   Console::Write( "String '{0}' is ", str1 );
   Console::Write( "{0} ", str );
   Console::WriteLine( "String '{0}'.", str2 );
}

/*
This example produces the following results:

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*/
// Sample for String.CompareOrdinal(String, String)
using System;

class Sample {
    public static void Main() {
    String str1 = "ABCD";
    String str2 = "abcd";
    String str;
    int result;

    Console.WriteLine();
    Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.");
    Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
    result = String.CompareOrdinal(str1, str2);
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("String '{0}' is ", str1);
    Console.Write("{0} ", str);
    Console.WriteLine("String '{0}'.", str2);
    }
}
/*
This example produces the following results:

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*/
' Sample for String.CompareOrdinal(String, String)
Class Sample
   Public Shared Sub Main()
      Dim str1 As [String] = "ABCD"
      Dim str2 As [String] = "abcd"
      Dim str As [String]
      Dim result As Integer
      
      Console.WriteLine()
      Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.")
      Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
      result = [String].CompareOrdinal(str1, str2)
      str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
      Console.Write("String '{0}' is ", str1)
      Console.Write("{0} ", str)
      Console.WriteLine("String '{0}'.", str2)
   End Sub
End Class
'
'This example produces the following results:
'
'Compare the numeric values of the corresponding Char objects in each string.
'str1 = 'ABCD', str2 = 'abcd'
'String 'ABCD' is less than String 'abcd'.
'

注解

此方法使用序号排序规则执行区分大小写的比较。This method performs a case-sensitive comparison using ordinal sort rules. 有关 word、字符串和序号排序的详细信息,请参阅 System.Globalization.CompareOptionsFor more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions. 若要使用序号排序规则执行不区分大小写的比较,请调用 Compare(String, String, StringComparison) 方法,将 comparisonType 参数设置为 StringComparison.OrdinalIgnoreCaseTo perform a case-insensitive comparison using ordinal sort rules, call the Compare(String, String, StringComparison) method with the comparisonType argument set to StringComparison.OrdinalIgnoreCase.

由于 CompareOrdinal(String, String) 是静态方法,因此可以 null``strAstrBBecause CompareOrdinal(String, String) is a static method, strA and strB can be null. 如果同时 null这两个值,则该方法将返回0(零),表示 strAstrB 相等。If both values are null, the method returns 0 (zero), which indicates that strA and strB are equal. 如果只 null其中一个值,则方法会将非 null 值视为更大的值。If only one of the values is null, the method considers the non-null value to be greater.

另请参阅

适用于