CompareOptions 枚举

定义

定义要用于 CompareInfo 的字符串比较选项。

此枚举支持其成员值的按位组合。

[System.Flags]
public enum CompareOptions
[System.Flags]
[System.Serializable]
public enum CompareOptions
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum CompareOptions
继承
CompareOptions
属性

字段

名称 说明
IgnoreCase 1

指示字符串比较必须忽略大小写。

IgnoreKanaType 8

指示字符串比较必须忽略 Kana 类型。 假名类型是指日语平假名和片假名字符,它们表示日语中的语音。 平假名用于表示日语自有的短语和字词,而片假名用于表示从其他语言借用的字词,如“computer”或“Internet”。 语音既可以用平假名也可以用片假名表示。 如果选择该值,则认为一个语音的平假名字符等于同一语音的片假名字符。

IgnoreNonSpace 2

指示字符串比较必须忽略不占空间的组合字符,比如音调符号。 Unicode 标准将组合字符定义为与基字符组合起来产生新字符的字符。 不占空间的组合字符在呈现时其本身不占用空间位置。

IgnoreSymbols 4

指示字符串比较必须忽略符号,如空格字符、标点、货币符号、百分号、数学符号、& 号等等。

IgnoreWidth 16

指示字符串比较必须忽略字符宽度。 例如,日语片假名字符可以写为全角或半角形式。 如果选择此值,则认为片假名字符的全角形式等同于半角形式。

None 0

指定字符串比较的默认选项设置。

Ordinal 1073741824

指示必须使用字符串的连续 Unicode UTF-16 编码值进行字符串比较(使用代码单元进行代码单元比较),这样可以提高比较速度,但不能区分区域性。 如果 XXXX16 小于 YYYY16,则以“XXXX16”代码单元开头的字符串位于以“YYYY16”代码单元开头的字符串之前。 此值必须单独使用,而不能与其他 CompareOptions 值组合在一起。

OrdinalIgnoreCase 268435456

字符串比较必须忽略大小写,然后执行序号比较。 此方法相当于先使用固定区域性将字符串转换为大写,然后再对结果执行序号比较。

StringSort 536870912

指示字符串比较必须使用字符串排序算法。 在字符串排序中,连字符、撇号以及其他非字母数字符号都排在字母数字字符之前。

示例

using System;
using System.Collections;
using System.Globalization;

public class SamplesCompareOptions  {

   private class MyStringComparer: IComparer {
      private CompareInfo myComp;
      private CompareOptions myOptions = CompareOptions.None;

      // Constructs a comparer using the specified CompareOptions.
      public MyStringComparer( CompareInfo cmpi, CompareOptions options )  {
         myComp = cmpi;
         this.myOptions = options;
      }

      // Compares strings with the CompareOptions specified in the constructor.
      public int Compare(Object a, Object b) {
         if (a == b) return 0;
         if (a == null) return -1;
         if (b == null) return 1;

         String sa = a as String;
         String sb = b as String;
         if (sa != null && sb != null)
            return myComp.Compare(sa, sb, myOptions);
         throw new ArgumentException("a and b should be strings.");
      }
   }

   public static void Main()  {

      // Creates and initializes an array of strings to sort.
      String[] myArr = new String[9] { "cant", "bill's", "coop", "cannot", "billet", "can't", "con", "bills", "co-op" };
      Console.WriteLine( "\nInitially," );
      foreach ( String myStr in myArr )
         Console.WriteLine( myStr );

      // Creates and initializes a Comparer to use.
      //CultureInfo myCI = new CultureInfo( "en-US", false );
      MyStringComparer myComp = new MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.None);

      // Sorts the array without StringSort.
      Array.Sort( myArr, myComp );
      Console.WriteLine( "\nAfter sorting without CompareOptions.StringSort:" );
      foreach ( String myStr in myArr )
         Console.WriteLine( myStr );

      // Sorts the array with StringSort.
      myComp = new MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.StringSort);
      Array.Sort( myArr, myComp );
      Console.WriteLine( "\nAfter sorting with CompareOptions.StringSort:" );
      foreach ( String myStr in myArr )
         Console.WriteLine( myStr );
   }
}

/*
This code produces the following output.

Initially,
cant
bill's
coop
cannot
billet
can't
con
bills
co-op

After sorting without CompareOptions.StringSort:
billet
bills
bill's
cannot
cant
can't
con
coop
co-op

After sorting with CompareOptions.StringSort:
bill's
billet
bills
can't
cannot
cant
co-op
con
coop

*/

注解

有关此 API 的详细信息,请参阅 CompareOptions 的补充 API 备注

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

另请参阅