String.CompareOrdinal 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
CompareOrdinal(String, String)
public:
static int CompareOrdinal(System::String ^ strA, System::String ^ strB);
public static int CompareOrdinal(string strA, 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
要比較的第一個字串。
- strB
- String
要比較的第二個字串。
傳回
一個整數表示兩個比較物之間的詞彙關係。
| 價值 | 條件 |
|---|---|
| 低於零 |
strA 小於 strB。
|
| 零 |
strA 且 strB 相等。
|
| 大於零 |
strA 大於 strB。
|
範例
以下範例將進行兩個字串的序數比較,兩者僅在情形下有所不同。
// 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)
open System
let str1 = "ABCD"
let str2 = "abcd"
printfn "\nCompare the numeric values of the corresponding Char objects in each string."
printfn $"str1 = '{str1}', str2 = '{str2}'"
let result = String.CompareOrdinal(str1, str2)
let str = if result < 0 then "less than" elif result > 0 then "greater than" else "equal to"
printf $"String '{str1}' is "
printf $"{str} "
printfn $"String '{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'.
'
備註
此方法使用序數排序規則進行大小寫區分比較。 關於字、字串和序數排序的更多資訊,請參見 System.Globalization.CompareOptions。 若要使用序數排序規則進行大小寫不區分的比較,請呼叫 Compare(String, String, StringComparison) 參數 comparisonType 設為 StringComparison.OrdinalIgnoreCase的方法。
因為 CompareOrdinal(String, String) 是一個靜態方法, strA 且 strB 可以是 null。 若兩個值皆為 null,則該方法回傳 0(零),表示 strA 和 strB 相等。 若只有其中一個值為 null,該方法將非空值視為較大。
另請參閱
適用於
CompareOrdinal(String, Int32, String, Int32, Int32)
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);
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
比較中第一個用的字串。
- indexA
- Int32
子字串的 strA起始索引。
- strB
- String
這是第二個用來比較的弦。
- indexB
- Int32
子字串的 strB起始索引。
- length
- Int32
子串中可比較的最大字元數。
傳回
一個32位元帶符號的整數,表示兩個比較元之間的詞彙關係。
| 價值 | 條件 |
|---|---|
| 低於零 | 子串 的 strA 比 中的 strB子串小。
|
| 零 | 子串相等,或 length 為零。
|
| 大於零 | 子串 在 strA 中 大於 的 strB子串。
|
例外狀況
strA不是null,且indexA大於strA......Length
-或-
strB不是null,且indexB大於strB......Length
-或-
indexA, , indexB或 length 為負值。
範例
以下範例展示了這點 CompareOrdinal ,並 Compare 使用不同的排序順序。
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]);
}
}
open System
open System.Globalization
[<EntryPoint>]
let main _ =
let strLow = "abc"
let strCap = "ABC"
let result = "equal to "
let pos = 1
// The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
let x = String.CompareOrdinal(strLow, pos, strCap, pos, 1)
let result =
if x < 0 then "less than"
elif x > 0 then "greater than"
else "equal to"
printfn $"CompareOrdinal(\"{strLow}\"[{pos}], \"{strCap}\"[{pos}]):"
printfn $" '{strLow[pos]}' is {result} '{strCap[pos]}'"
// In U.S. English culture, 'b' is linguistically less than 'B'.
let x = String.Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo("en-US"))
let result =
if x < 0 then "less than"
elif x > 0 then "greater than"
else "equal to"
printfn $"Compare(\"{strLow}\"[{pos}], \"{strCap}\"[{pos}]):"
printfn $" '{strLow[pos]}' is {result} '{strCap[pos]}'"
0
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
備註
這些 indexA、 indexB、 和 length 參數必須為非負。
比較的字元數取較小的字元數,取strA較小indexAstrBindexB的長度,且。length
此方法使用序數排序規則進行大小寫區分比較。 關於字、字串和序數排序的更多資訊,請參見 System.Globalization.CompareOptions。 若要使用序數排序規則進行大小寫不區分的比較,請呼叫 Compare(String, Int32, String, Int32, Int32, StringComparison) 參數 comparisonType 設為 StringComparison.OrdinalIgnoreCase的方法。
因為 CompareOrdinal(String, String) 是一個靜態方法, strA 且 strB 可以是 null。 若兩個值皆為 null,則該方法回傳 0(零),表示 strA 和 strB 相等。 若只有其中一個值為 null,該方法將非空值視為較大。