System.String.Compare 方法 (String, Int32, String, Int32, Int32, StringComparison)

方法描述

使用指定的规则比较两个指定的 String 对象的子字符串,并返回一个整数,指示二者在排序顺序中的相对位置。

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

public static int Compare(
	string strA,
	int indexA,
	string strB,
	int indexB,
	int length,
	StringComparison comparisonType
)

参数/返回值

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

提示和注释

strA 和 strB 中要比较的子字符串分别从 indexA 和 indexB 开始。 indexA 和 indexB 都是从零开始的;也就是说,strA 和 strB 的第一个字符在位置 0,而不是位置 1。 第一个子字符串的长度等于 strA 的长度减 indexA 加一。 第二个子字符串的长度等于 strB 的长度减 indexB 加一。

要比较的字符数是两个子字符串的长度和 length 中较少的那一个。 indexA、indexB 和 length 参数必须为非负数。

comparisonType 参数指示比较是应使用当前区域性还是固定区域性,是应考虑还是应忽略比较字的大小写,是使用字(区分区域性)排序规则还是序号(不区分区域性)排序规则。

一个或者两个比较字都可以是 null。 根据定义,任何字符串(包括空字符串 (""))的比较结果都大于 null 引用;两个 null 引用的比较结果为相等。

当发现不相等或已经比较了两个子字符串时,比较就会终止。 但是,如果两个字符串一直比较到其中一个字符串的末尾时仍相同,而另一个字符串仍有剩余字符,则认为仍有剩余字符的字符串较大。 返回值为执行最后一次比较所得的结果。

当比较受区域性特定的大小写规则影响时,可能会发生意外结果。 例如,在土耳其语中,下面的示例将产生错误结果,因为土耳其语的文件系统不会对“file”中的字母“i”使用语义大小写规则。

C#

C++

VB

复制

static bool IsFileURI(String path)

{

return (String.Compare(path, 0, "file:", 0, 5, true) == 0);

}

使用序号比较比较路径名与“file”。 执行此操作的正确代码如下:

C#

C++

VB

复制

static bool IsFileURI(String path)

{

return (String.Compare(path, 0, "file:", 0, 5, StringComparison.OrdinalIgnoreCase) == 0);

}

System.String.Compare 方法 (String, Int32, String, Int32, Int32, StringComparison)例子

下面的示例比较两个子字符串。

// Sample for String.Compare(String, Int32, String, Int32, Int32)
using System;

class Sample {
    public static void Main() {
//                 0123456
    String str1 = "machine";
    String str2 = "device";
    String str;
    int result;

    Console.WriteLine();
    Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
    result = String.Compare(str1, 2, str2, 0, 2);
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
    Console.Write("{0} ", str);
    Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
    }
}
/*
This example produces the following results:

str1 = 'machine', str2 = 'device'
Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
*/

异常

异常 异常描述
ArgumentOutOfRangeException
  • indexA 大于 strA.Length。
  • indexB 大于 strB.Length。
  • indexA、indexB 或 length 为负。
  • indexA 或 indexB 为 null,而 length 大于零。
ArgumentException comparisonType 不是一个 StringComparison 值。

命名空间

namespace: System

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

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.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 系统要求。