String.CompareOrdinal String.CompareOrdinal String.CompareOrdinal String.CompareOrdinal Method

定義

藉由評估每個字串中對應的 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) CompareOrdinal(String, Int32, String, Int32, Int32) CompareOrdinal(String, Int32, String, Int32, Int32) 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) CompareOrdinal(String, String) CompareOrdinal(String, String) 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) CompareOrdinal(String, Int32, String, Int32, Int32) CompareOrdinal(String, Int32, String, Int32, Int32) 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 String String String

要用於比較中的第一個字串。The first string to use in the comparison.

indexA
Int32 Int32 Int32 Int32

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

strB
String String String String

要用於比較的第二個字串。The second string to use in the comparison.

indexB
Int32 Int32 Int32 Int32

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

length
Int32 Int32 Int32 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 不是 nullindexA 大於 strA.LengthstrA is not null and indexA is greater than strA.Length.

-或--or- strB 不是 nullindexB 大於 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
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 'Main
End Class 'Test

備註

indexAindexB,和length參數不可為負值。The indexA, indexB, and length parameters must be nonnegative.

相較的字元數是較小的長度strAindexA,長度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. 如需有關字、 字串和序數排序的詳細資訊,請參閱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)是一種靜態方法,strAstrB可以是nullBecause 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) CompareOrdinal(String, String) CompareOrdinal(String, String) 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 String String String

要比較的第一個字串。The first string to compare.

strB
String String String String

要比較的第二個字串。The second string to compare.

傳回

整數,表示兩個比較元 (Comparand) 之間的語彙關係。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)
Imports System
Imports Microsoft.VisualBasic

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 'Main
End Class 'Sample
'
'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. 如需有關字、 字串和序數排序的詳細資訊,請參閱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)是一種靜態方法,strAstrB可以是nullBecause 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.

另請參閱

適用於