System.StringComparer 类

方法描述

表示一种字符串比较操作,该操作使用特定的大小写以及基于区域性的比较规则或序号比较规则。

语法定义(C# System.StringComparer 类 的用法)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class StringComparer : IComparer, 
	IEqualityComparer, IComparer, IEqualityComparer

构造函数

构造函数名称 构造函数描述
StringComparer 初始化 StringComparer 类的新实例。

成员/方法

方法名称 方法描述
Compare(Object, Object) 当在派生类中重写时,将比较两个对象并返回其相对排序顺序的指示。
Compare(String, String) 当在派生类中重写时,将比较两个字符串并返回其相对排序顺序的指示。
Create 创建 StringComparer 对象,该对象根据指定区域性的规则对字符串进行比较。
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Equals(Object, Object) 当在派生类中重写时,指示两个对象是否相等。
Equals(String, String) 当在派生类中重写时,指示两个字符串是否相等。
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetHashCode() 用作特定类型的哈希函数。 (继承自 Object。)
GetHashCode(Object) 当在派生类中重写时,将获取指定对象的哈希代码。
GetHashCode(String) 当在派生类中重写时,将获取指定字符串的哈希代码。
GetType 获取当前实例的 Type。 (继承自 Object。)
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
ToString 返回表示当前对象的字符串。 (继承自 Object。)

提示和注释

从 StringComparer 类派生的对象包含基于字符串的比较操作、相等操作和哈希代码操作,这些操作同时考虑大小写和特定于区域性的比较规则。 可以使用 StringComparer 类创建一个特定于类型的比较,以对泛型集合中的元素进行排序。 Hashtable 、Dictionary、SortedList 和 SortedList 等类使用 StringComparer 类进行排序。

由 StringComparer 类表示的比较操作可定义为区分大小写或不区分大小写,并可以使用单词(区分区域性)或序号(不区分区域性)比较规则。 有关单词和序号比较规则的更多信息,请参见 System.Globalization.CompareOptions。

实现的属性

您可能会对如何使用 StringComparer 类属性感到困惑,因为这其中似乎存在一个矛盾。 StringComparer 类被声明为 abstract(Visual Basic 中为 MustInherit),这意味着只能对从 StringComparer 类派生的类的对象调用它的成员。 矛盾在于 StringComparer 类的每个属性都被声明为 static(Visual Basic 中为 Shared),这意味着不必先创建一个派生类即可调用这些属性。

您可以直接调用 StringComparer 属性,因为每个属性实际上返回的是从 StringComparer 类派生的匿名类的实例。 因此,每个属性值的类型是 StringComparer,它是匿名类的基类,而不是匿名类自身的类型。 每个 StringComparer 类属性都返回支持预定义大小写和比较规则的 StringComparer 对象。

System.StringComparer 类例子

此示例演示不同的 StringComparer 对象如何对三种版本的拉丁字母 I 进行排序。

// This example demonstrates members of the 
// System.StringComparer class.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
// Create a list of string.
    List list = new List();

// Get the tr-TR (Turkish-Turkey) culture.
    CultureInfo turkish = new CultureInfo("tr-TR");

// Get the culture that is associated with the current thread.
    CultureInfo thisCulture = Thread.CurrentThread.CurrentCulture;

// Get the standard StringComparers.
    StringComparer invCmp =   StringComparer.InvariantCulture;
    StringComparer invICCmp = StringComparer.InvariantCultureIgnoreCase;
    StringComparer currCmp = StringComparer.CurrentCulture;
    StringComparer currICCmp = StringComparer.CurrentCultureIgnoreCase;
    StringComparer ordCmp = StringComparer.Ordinal;
    StringComparer ordICCmp = StringComparer.OrdinalIgnoreCase;

// Create a StringComparer that uses the Turkish culture and ignores case.
    StringComparer turkICComp = StringComparer.Create(turkish, true);

// Define three strings consisting of different versions of the letter I.
// LATIN CAPITAL LETTER I (U+0049)
    string capitalLetterI = "I";  

// LATIN SMALL LETTER I (U+0069)
    string smallLetterI   = "i";

// LATIN SMALL LETTER DOTLESS I (U+0131)
    string smallLetterDotlessI = "\u0131";

// Add the three strings to the list.
    list.Add(capitalLetterI);
    list.Add(smallLetterI);
    list.Add(smallLetterDotlessI);

// Display the original list order.
    Display(list, "The original order of the list entries...");

// Sort the list using the invariant culture.
    list.Sort(invCmp);
    Display(list, "Invariant culture...");
    list.Sort(invICCmp);
    Display(list, "Invariant culture, ignore case...");

// Sort the list using the current culture.
    Console.WriteLine("The current culture is \"{0}\".", thisCulture.Name);
    list.Sort(currCmp);
    Display(list, "Current culture...");
    list.Sort(currICCmp);
    Display(list, "Current culture, ignore case...");

// Sort the list using the ordinal value of the character code points.
    list.Sort(ordCmp);
    Display(list, "Ordinal...");
    list.Sort(ordICCmp);
    Display(list, "Ordinal, ignore case...");

// Sort the list using the Turkish culture, which treats LATIN SMALL LETTER 
// DOTLESS I differently than LATIN SMALL LETTER I.
    list.Sort(turkICComp);
    Display(list, "Turkish culture, ignore case...");
    }

    public static void Display(List lst, string title)
    {
    Char c;
    int  codePoint;
    Console.WriteLine(title);
    foreach (string s in lst)
        {
        c = s[0];
        codePoint = Convert.ToInt32(c);
        Console.WriteLine("0x{0:x}", codePoint); 
        }
    Console.WriteLine();
    }
}
/*
This code example produces the following results:

The original order of the list entries...
0x49
0x69
0x131

Invariant culture...
0x69
0x49
0x131

Invariant culture, ignore case...
0x49
0x69
0x131

The current culture is "en-US".
Current culture...
0x69
0x49
0x131

Current culture, ignore case...
0x49
0x69
0x131

Ordinal...
0x49
0x69
0x131

Ordinal, ignore case...
0x69
0x49
0x131

Turkish culture, ignore case...
0x131
0x49
0x69

*/

继承层次结构

System.Object

System.StringComparer

命名空间

namespace: System

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

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

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