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

方法描述

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

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

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

参数/返回值

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

提示和注释

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, String, StringComparison)例子

比较结果受以下因素的影响:区域性的选择,是否忽略大小写,是否执行序号比较。

// This example demonstrates the 
// System.String.Compare(String, String, StringComparison) method.

using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
    string intro = "Compare three versions of the letter I using different " + 
                   "values of StringComparison.";

// Define an array of strings where each element contains a version of the 
// letter I. (An array of strings is used so you can easily modify this 
// code example to test additional or different combinations of strings.)  

    string[] threeIs = new string[3];
// LATIN SMALL LETTER I (U+0069)
    threeIs[0] = "\u0069";
// LATIN SMALL LETTER DOTLESS I (U+0131)
    threeIs[1] = "\u0131";
// LATIN CAPITAL LETTER I (U+0049)
    threeIs[2] = "\u0049";

    string[] unicodeNames = 
             {
             "LATIN SMALL LETTER I (U+0069)", 
             "LATIN SMALL LETTER DOTLESS I (U+0131)", 
             "LATIN CAPITAL LETTER I (U+0049)"
             };

    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

//
    Console.Clear();
    Console.WriteLine(intro);

// Display the current culture because the culture-specific comparisons
// can produce different results with different cultures.
    Console.WriteLine("The current culture is {0}.\n", 
                       Thread.CurrentThread.CurrentCulture.Name);

// Determine the relative sort order of three versions of the letter I. 
    foreach (StringComparison sc in scValues)
        {
        Console.WriteLine("StringComparison.{0}:", sc);

// LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131)
        Test(0, 1, sc, threeIs, unicodeNames);

// LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049)
        Test(0, 2, sc, threeIs, unicodeNames);

// LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049)
        Test(1, 2, sc, threeIs, unicodeNames);

        Console.WriteLine();
        }
    }

    protected static void Test(int x, int y, 
                               StringComparison comparison, 
                               string[] testI, string[] testNames)
    {
    string resultFmt = "{0} is {1} {2}";
    string result = "equal to";
    int cmpValue = 0;
//
    cmpValue = String.Compare(testI[x], testI[y], comparison);
    if      (cmpValue < 0) 
        result = "less than";
    else if (cmpValue > 0)
        result = "greater than";
    Console.WriteLine(resultFmt, testNames[x], result, testNames[y]);
    }
}

/*
This code example produces the following results:

Compare three versions of the letter I using different values of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.CurrentCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.InvariantCulture:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.InvariantCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.Ordinal:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.OrdinalIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

*/

异常

异常 异常描述
ArgumentException comparisonType 不是一个 StringComparison 值。
NotSupportedException 不支持 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 系统要求。