System.String.CompareOrdinal 方法 (String, Int32, String, Int32, Int32)

方法描述

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

语法定义(C# System.String.CompareOrdinal 方法 (String, Int32, String, Int32, Int32) 的用法)

public static int CompareOrdinal(
	string strA,
	int indexA,
	string strB,
	int indexB,
	int length
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
strA System-String 要在比较中使用的第一个字符串。
indexA System-Int32 strA 中子字符串的起始索引。
strB System-String 要在比较中使用的第二个字符串。
indexB System-Int32 strB 中子字符串的起始索引。
length System-Int32 要比较的子字符串中字符的最大数量。
返回值 System.Int32 一个 32 位带符号整数,指示两个比较数之间的词法关系。 值 Condition 小于零 strA 中的子字符串小于 strB 中的子字符串。 零 子字符串相等,或者 length 为零。 大于零 strA 中的子字符串大于 strB 中的子字符串。

提示和注释

indexA、indexB 和 length 参数必须为非负数。

所比较的字符数是 strA 的长度减去 indexA、strB 的长度减去 indexB 和 length 这三者中最小的一个。

此方法使用序号排序规则执行区分大小写的比较。 有关字、字符串和顺序排序的更多信息,请参见 System.Globalization.CompareOptions。 若要使用序号排序规则执行不区分大小写的比较,请调用带 OrdinalIgnoreCase 的 StringComparison 值的 Compare 方法。

System.String.CompareOrdinal 方法 (String, Int32, String, Int32, Int32)例子

下面的示例演示 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]);
	}
}

异常

异常 异常描述
ArgumentOutOfRangeException
  • indexA 大于 strA。 Length .
  • indexB 大于 strB。 Length .
  • indexA、indexB 或 length 为负。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。