CompareInfo 类

定义

实现一组区分区域性的字符串比较方法。

public ref class CompareInfo
public ref class CompareInfo sealed : System::Runtime::Serialization::IDeserializationCallback
public ref class CompareInfo : System::Runtime::Serialization::IDeserializationCallback
public class CompareInfo
public sealed class CompareInfo : System.Runtime.Serialization.IDeserializationCallback
public class CompareInfo : System.Runtime.Serialization.IDeserializationCallback
[System.Serializable]
public class CompareInfo : System.Runtime.Serialization.IDeserializationCallback
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class CompareInfo : System.Runtime.Serialization.IDeserializationCallback
type CompareInfo = class
type CompareInfo = class
    interface IDeserializationCallback
[<System.Serializable>]
type CompareInfo = class
    interface IDeserializationCallback
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CompareInfo = class
    interface IDeserializationCallback
Public Class CompareInfo
Public NotInheritable Class CompareInfo
Implements IDeserializationCallback
Public Class CompareInfo
Implements IDeserializationCallback
继承
CompareInfo
属性
实现

注解

用于比较和排序数据的约定因区域性而异。 例如,排序顺序可能基于拼音或字符的可视表示形式。 在东亚语言中,按文字的笔画和部首对字符进行排序。 排序还取决于语言和文化所使用的字母表顺序。 例如,丹麦语有一个“Æ”字符,它在字母表中排在“Z”之后。 此外,比较可以区分大小写或不区分大小写,并且大小写规则也可能因区域性而异。 CompareInfo 类负责维护此区分区域性的字符串比较数据并执行区分区域性的字符串运算。

通常,不必直接实例化对象,因为所有非序号字符串比较作(包括对方法的CompareInfo调用)都隐式使用一个String.Compare对象。 但是,如果确实要检索对象 CompareInfo ,可以通过以下方式之一执行此作:

忽略的搜索值

字符集包括可忽略字符,这些字符是执行语言或区域性敏感比较时不考虑的字符。 当比较方法(如 IndexOfLastIndexOf)执行区分区域性的比较时,它们不会考虑此类字符。 可忽略的字符包括:

  • String.Empty。 文化敏感比较方法将始终在被搜索字符串的开头(索引零)找到空字符串。

  • 字符或由多个字符和运算中未考虑的码位(因为比较选项)构成的字符串,具体而言,CompareOptions.IgnoreNonSpaceCompareOptions.IgnoreSymbols 选项生成忽略符号和非间距组合字符的搜索。

  • 具有没有语言意义的代码点的字符串。 例如,始终在区分区域性的字符串比较中忽略软连字符 (U+00AD)。

安全注意事项

如果安全决策取决于字符串比较或大小写更改,则您应使用 InvariantCulture 属性来确保行为一致,无论操作系统的区域性设置如何。

注释

如果可能,应使用具有类型 CompareOptions 参数的字符串比较方法来指定预期的比较类型。 作为一般规则,对于在用户界面中显示的字符串比较,请使用语言选项(使用当前文化)并指定 OrdinalOrdinalIgnoreCase 用于安全比较。

示例

  The following example shows how the <xref:System.Globalization.CompareInfo> object associated with a <xref:System.Globalization.CultureInfo> object affects string comparison.

  :::code language="csharp" source="~/snippets/csharp/System.Globalization/CompareInfo/Overview/CompareInfo.cs" id="Snippet1":::
  :::code language="vb" source="~/snippets/visualbasic/System.Globalization/CompareInfo/Overview/CompareInfo.vb" id="Snippet1":::

属性

名称 说明
LCID

获取当前 CompareInfo格式正确的区域性标识符。

Name

获取用于按此 CompareInfo 对象对操作进行排序的区域性的名称。

Version

获取有关用于比较和排序字符串的 Unicode 版本的信息。

方法

名称 说明
Compare(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

比较两个只读字符范围。

Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions)

使用指定 CompareOptions 值将一个字符串的节与另一个字符串的节进行比较。

Compare(String, Int32, Int32, String, Int32, Int32)

将一个字符串的节与另一个字符串的节进行比较。

Compare(String, Int32, String, Int32, CompareOptions)

使用指定 CompareOptions 值将字符串的结束部分与另一个字符串的结束部分进行比较。

Compare(String, Int32, String, Int32)

将字符串的结束部分与另一个字符串的结束部分进行比较。

Compare(String, String, CompareOptions)

使用指定的 CompareOptions 值比较两个字符串。

Compare(String, String)

比较两个字符串。

Equals(Object)

确定指定的对象是否等于当前 CompareInfo 对象。

GetCompareInfo(Int32, Assembly)

初始化与指定区域性关联的新 CompareInfo 对象,并在指定的 Assembly区域性中使用字符串比较方法。

GetCompareInfo(Int32)

使用指定的标识符初始化与区域性关联的新 CompareInfo 对象。

GetCompareInfo(String, Assembly)

初始化与指定区域性关联的新 CompareInfo 对象,并在指定的 Assembly区域性中使用字符串比较方法。

GetCompareInfo(String)

使用指定名称初始化与区域性关联的新 CompareInfo 对象。

GetHashCode()

用作哈希算法和数据结构的当前 CompareInfo 哈希函数,例如哈希表。

GetHashCode(ReadOnlySpan<Char>, CompareOptions)

根据指定的比较选项获取字符范围的哈希代码。

GetHashCode(String, CompareOptions)

根据指定的比较选项获取字符串的哈希代码。

GetSortKey(ReadOnlySpan<Char>, Span<Byte>, CompareOptions)

计算指定输入的排序键。

GetSortKey(String, CompareOptions)

SortKey使用指定的值获取指定CompareOptions字符串的对象。

GetSortKey(String)

获取指定字符串的排序键。

GetSortKeyLength(ReadOnlySpan<Char>, CompareOptions)

获取从指定输入生成的排序键字节总数。

GetType()

获取当前实例的 Type

(继承自 Object)
IndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

搜索源字符串中子字符串的第一个匹配项。

IndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

在指定的只读字符范围内搜索子字符串的第一个匹配项。

IndexOf(ReadOnlySpan<Char>, Rune, CompareOptions)

搜索指定只读字符范围内的第一个 Rune 匹配项。

IndexOf(String, Char, CompareOptions)

搜索指定的字符,并使用指定的 CompareOptions 值返回整个源字符串中第一个匹配项的从零开始的索引。

IndexOf(String, Char, Int32, CompareOptions)

搜索指定的字符并返回源字符串节中第一个匹配项的从零开始的索引,该索引使用指定的 CompareOptions 值从指定的索引扩展到字符串的末尾。

IndexOf(String, Char, Int32, Int32, CompareOptions)

搜索指定的字符并返回源字符串的节中第一个匹配项的从零开始的索引,该索引从指定索引开始,并包含使用指定值的指定 CompareOptions 数量的元素。

IndexOf(String, Char, Int32, Int32)

搜索指定的字符,并返回源字符串节中从指定索引开始的第一个匹配项的从零开始的索引,并包含指定的元素数。

IndexOf(String, Char, Int32)

搜索指定的字符,并返回源字符串节中从指定索引扩展到字符串末尾的第一个匹配项的从零开始的索引。

IndexOf(String, Char)

搜索指定的字符,并返回整个源字符串中第一个匹配项的从零开始的索引。

IndexOf(String, String, CompareOptions)

搜索指定的子字符串,并使用指定的 CompareOptions 值返回整个源字符串中第一个匹配项的从零开始的索引。

IndexOf(String, String, Int32, CompareOptions)

搜索指定的子字符串,并使用指定的值返回源字符串节中第一个匹配项的从零开始的索引,该索引从指定的 CompareOptions 索引扩展到字符串的末尾。

IndexOf(String, String, Int32, Int32, CompareOptions)

搜索指定的子字符串,并返回从指定索引开始的源字符串节中第一个匹配项的从零开始的索引,并使用指定的值包含指定的 CompareOptions 元素数。

IndexOf(String, String, Int32, Int32)

搜索指定的子字符串,并返回源字符串节中从指定索引开始且包含指定数量的元素的第一个匹配项的从零开始的索引。

IndexOf(String, String, Int32)

搜索指定的子字符串,并返回源字符串节中从指定索引扩展到字符串末尾的第一个匹配项的从零开始的索引。

IndexOf(String, String)

搜索指定的子字符串,并返回整个源字符串中第一个匹配项的从零开始的索引。

IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

确定字符串是否以特定前缀开头。

IsPrefix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

确定字符的只读范围是否以特定前缀开头。

IsPrefix(String, String, CompareOptions)

确定指定的源字符串是否使用指定的 CompareOptions 值以指定的前缀开头。

IsPrefix(String, String)

确定指定的源字符串是否以指定的前缀开头。

IsSortable(Char)

指示指定的 Unicode 字符是否可排序。

IsSortable(ReadOnlySpan<Char>)

指示指定的 Unicode 只读字符范围是否可排序。

IsSortable(Rune)

指示指定的 Rune 是否可排序。

IsSortable(String)

指示指定的 Unicode 字符串是否可排序。

IsSuffix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

确定字符串是否以特定后缀结尾。

IsSuffix(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

确定字符的只读范围是否以特定的后缀结尾。

IsSuffix(String, String, CompareOptions)

确定指定的源字符串是否使用指定的值以指定的 CompareOptions 后缀结尾。

IsSuffix(String, String)

确定指定的源字符串是否以指定的后缀结尾。

LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions, Int32)

搜索源字符串中子字符串的最后一个匹配项。

LastIndexOf(ReadOnlySpan<Char>, ReadOnlySpan<Char>, CompareOptions)

在指定的只读字符范围内搜索子字符串的最后一个匹配项。

LastIndexOf(ReadOnlySpan<Char>, Rune, CompareOptions)

搜索指定只读字符范围内的最后一个 Rune 匹配项。

LastIndexOf(String, Char, CompareOptions)

搜索指定的字符,并使用指定的 CompareOptions 值返回整个源字符串中最后一个匹配项的从零开始的索引。

LastIndexOf(String, Char, Int32, CompareOptions)

搜索指定的字符,并返回源字符串节中最后一个匹配项的从开始到指定索引(使用指定值从字符串开头扩展到指定 CompareOptions 索引)的从零开始的索引。

LastIndexOf(String, Char, Int32, Int32, CompareOptions)

搜索指定的字符,并返回源字符串节中最后一个匹配项的从零开始的索引,该节包含指定数量的元素,并使用指定的值在指定 CompareOptions 索引处结束。

LastIndexOf(String, Char, Int32, Int32)

搜索指定的字符,并返回源字符串的节中最后一个匹配项的从零开始的索引,该节包含指定数量的元素并在指定索引处结束。

LastIndexOf(String, Char, Int32)

搜索指定的字符,并返回源字符串节中从字符串开头扩展到指定索引的最后一个匹配项的从零开始的索引。

LastIndexOf(String, Char)

搜索指定的字符,并返回整个源字符串中最后一个匹配项的从零开始的索引。

LastIndexOf(String, String, CompareOptions)

搜索指定的子字符串,并使用指定的 CompareOptions 值返回整个源字符串中最后一个匹配项的从零开始的索引。

LastIndexOf(String, String, Int32, CompareOptions)

搜索指定的子字符串,并返回源字符串的节中最后一个匹配项的从零开始的索引,该索引从字符串的开头扩展到使用指定的值指定的 CompareOptions 索引。

LastIndexOf(String, String, Int32, Int32, CompareOptions)

搜索指定的子字符串,并使用指定的值返回源字符串节中最后一个匹配项的从零开始的索引,该索引包含指定数量的元素,并在指定 CompareOptions 索引处结束。

LastIndexOf(String, String, Int32, Int32)

搜索指定的子字符串,并返回源字符串节中最后一个匹配项的从零开始的索引,该节包含指定数量的元素,并在指定索引处结束。

LastIndexOf(String, String, Int32)

搜索指定的子字符串,并在源字符串的节中返回从字符串开头扩展到指定索引的最后一个匹配项的从零开始的索引。

LastIndexOf(String, String)

搜索指定的子字符串,并返回整个源字符串中最后一个匹配项的从零开始的索引。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前 CompareInfo 对象的字符串。

显式接口实现

名称 说明
IDeserializationCallback.OnDeserialization(Object)

反序列化整个对象图时运行。

扩展方法

名称 说明
GetStringComparer(CompareInfo, CompareOptions)

StringComparer基于指定CompareInfo对象的区域性敏感字符串比较规则返回对象。

适用于

另请参阅