String.CompareOrdinal 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
CompareOrdinal(String, String) | |
CompareOrdinal(String, Int32, String, Int32, Int32) |
CompareOrdinal(String, String)
- Source:
- String.Comparison.cs
- Source:
- String.Comparison.cs
- Source:
- String.Comparison.cs
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 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)
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
,则该方法认为非 null 值更大。
另请参阅
适用于
CompareOrdinal(String, Int32, String, Int32, Int32)
- Source:
- String.Comparison.cs
- Source:
- String.Comparison.cs
- Source:
- String.Comparison.cs
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
为负数。
示例
以下示例演示和 CompareOrdinalCompare 使用不同的排序顺序。
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]);
}
}
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
少, 长度strB
越少indexB
, 和 length
indexA
。
此方法使用序号排序规则执行区分大小写的比较。 有关单词、字符串和序号排序的详细信息,请参阅 System.Globalization.CompareOptions。 若要使用序号排序规则执行不区分大小写的比较,请调用 方法,Compare(String, Int32, String, Int32, Int32, StringComparison)comparisonType
并将 参数设置为 StringComparison.OrdinalIgnoreCase。
因为 CompareOrdinal(String, String) 是静态方法, strA
并且可以 strB
是 null
。 如果两个值为 null
,则该方法返回 0 (零) ,这指示 strA
和 strB
相等。 如果只有一个值是 null
,则该方法认为非 null 值更大。