System.String.Compare 方法 (String, Int32, String, Int32, Int32, Boolean, CultureInfo)

方法描述

比较两个指定的 String 对象的子字符串(其中忽略或考虑其大小写,并使用区域性特定的信息干预比较),并返回一个整数,指示二者在排序顺序中的相对位置。

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

public static int Compare(
	string strA,
	int indexA,
	string strB,
	int indexB,
	int length,
	bool ignoreCase,
	CultureInfo culture
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
strA System-String 要在比较中使用的第一个字符串。
indexA System-Int32 strA 中子字符串的位置。
strB System-String 要在比较中使用的第二个字符串。
indexB System-Int32 strB 中子字符串的位置。
length System-Int32 要比较的子字符串中字符的最大数量。
ignoreCase System-Boolean 要在比较过程中忽略大小写,则为 true;否则为 false。
culture System-Globalization-CultureInfo 一个对象,提供区域性特定的比较信息。
返回值 System.Int32 一个整数,指示两个比较字之间的词法关系。 值 Condition 小于零 strA 中的子字符串小于 strB 中的子字符串。 零 子字符串相等,或者 length 为零。 大于零 strA 中的子字符串大于 strB 中的子字符串。

提示和注释

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

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

该比较使用 culture 参数来获取区域性特定的信息,如大小写规则和各个字符的字母顺序。 例如,区域性可以指定将某些字符的组合视为单个字符,或者用特殊方法比较大写和小写字符,或者字符的排序顺序取决于它前面或后面的字符。

使用字排序规则执行比较。 有关字、字符串和顺序排序的更多信息,请参见 System.Globalization.CompareOptions。

一个或者两个比较字都可以是 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, Boolean, CultureInfo)例子

区域性的选择会影响如何比较字母“I”。

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

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

    Console.WriteLine();
    Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
    Console.WriteLine("Ignore case, Turkish culture:");
    result = String.Compare(str1, 4, str2, 4, 2, true, new CultureInfo("tr-TR"));
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4, 2), str1);
    Console.Write("{0} ", str);
    Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4, 2), str2);

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

str1 = 'MACHINE', str2 = 'machine'
Ignore case, Turkish culture:
Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'.

Ignore case, invariant culture:
Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
*/

异常

异常 异常描述
ArgumentOutOfRangeException
  • indexA 大于 strA.Length。
  • indexB 大于 strB.Length。
  • indexA、indexB 或 length 为负。
  • strA 或 strB 为 null,而 length 大于零。
ArgumentNullException culture 为 null。

命名空间

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 系统要求。