使用英语阅读

通过


String.CompareOrdinal 方法

定义

重要

一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。

通过计算每个字符串中相应 String 对象的数值来比较两个 Char 对象。

重载

CompareOrdinal(String, String)

通过计算每个字符串中相应 String 对象的数值来比较两个指定的 Char 对象。

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

通过计算每个子字符串中相应 String 对象的数值来比较两个指定的 Char 对象的子字符串。

CompareOrdinal(String, String)

Source:
String.Comparison.cs
Source:
String.Comparison.cs
Source:
String.Comparison.cs

通过计算每个字符串中相应 String 对象的数值来比较两个指定的 Char 对象。

C#
public static int CompareOrdinal(string strA, string strB);
C#
public static int CompareOrdinal(string? strA, string? strB);

参数

strA
String

要比较的第一个字符串。

strB
String

要比较的第二个字符串。

返回

一个整数,指示两个比较字之间的词法关系。

“值” 条件
小于零 strA 小于 strB
strAstrB 相等。
大于零 strA 大于 strB

示例

以下示例对两个字符串执行和序号比较,这些字符串仅在大小写上不同。

C#
// 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'.
*/

注解

此方法使用序号排序规则执行区分大小写的比较。 有关单词、字符串和序号排序的详细信息,请参阅 System.Globalization.CompareOptions。 若要使用序号排序规则执行不区分大小写的比较,请调用 方法,Compare(String, String, StringComparison)comparisonType并将 参数设置为 StringComparison.OrdinalIgnoreCase

因为 CompareOrdinal(String, String) 是静态方法, strA 并且可以 strBnull。 如果两个值为 null,则该方法返回 0 (零) ,这指示 strAstrB 相等。 如果只有一个值是 null,则该方法认为非 null 值更大。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
String.Comparison.cs
Source:
String.Comparison.cs
Source:
String.Comparison.cs

通过计算每个子字符串中相应 String 对象的数值来比较两个指定的 Char 对象的子字符串。

C#
public static int CompareOrdinal(string strA, int indexA, string strB, int indexB, int length);
C#
public static int CompareOrdinal(string? strA, int indexA, string? strB, int indexB, int length);

参数

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

indexAindexBlength 为负数。

示例

以下示例演示和 CompareOrdinalCompare 使用不同的排序顺序。

C#
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]);
    }
}

注解

indexAindexBlength 参数必须是非否定的。

比较的字符数是长度越少越strA少, 长度strB越少indexB, 和 lengthindexA

此方法使用序号排序规则执行区分大小写的比较。 有关单词、字符串和序号排序的详细信息,请参阅 System.Globalization.CompareOptions。 若要使用序号排序规则执行不区分大小写的比较,请调用 方法,Compare(String, Int32, String, Int32, Int32, StringComparison)comparisonType并将 参数设置为 StringComparison.OrdinalIgnoreCase

因为 CompareOrdinal(String, String) 是静态方法, strA 并且可以 strBnull。 如果两个值为 null,则该方法返回 0 (零) ,这指示 strAstrB 相等。 如果只有一个值是 null,则该方法认为非 null 值更大。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0