CompareOptions 列舉

定義

定義與 CompareInfo 一起使用的字串比較選項。

此列舉支援其成員值的位元組合。

C#
[System.Flags]
public enum CompareOptions
C#
[System.Flags]
[System.Serializable]
public enum CompareOptions
C#
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum CompareOptions
繼承
CompareOptions
屬性

欄位

名稱 Description
IgnoreCase 1

指示字串比較必須忽略大小寫。

IgnoreKanaType 8

指示字串比較必須忽略假名類型。 假名類型意指日文平假名和片假名字元,表示日本語言中的語音。 平假名用於本土日文的語句和字詞,而片假名則用於自其他語言引進的字詞,例如「computer」或「Internet」。 平假名和片假名都可以用來表達語音。 如果選取這個值,就會將代表一個語音的平假名字元視為等於代表相同語音的片假名字元。

IgnoreNonSpace 2

指示字串比較必須忽略無間距的組合字元,例如變音符號。 Unicode Standard (Unicode 標準),將組合字元定義為結合基底字元以產生新字元的字元。 無間距的組合字元在呈現時本身並不佔用間距位置。

IgnoreSymbols 4

指示字串比較必須忽略符號,例如空白字元、標點符號、貨幣符號、百分比符號、數學符號、& 符號等等。

IgnoreWidth 16

指示字串比較必須忽略字元寬度。 例如,日文片假名字元可以書寫為全型或半型。 如果選取這個值,則片假名字元會書寫為全型並視為等同於以半型書寫的相同字元。

None 0

指示字串比較的預設選項設定值。

Ordinal 1073741824

表示字串比較必須使用字串的連續 Unicode UTF-16 編碼值 (逐一程式碼單位比較),這是快速的比較但不區分文化特性。 如果程式碼單位 XXXX16 小於 YYYY16,則以 XXXX16 開始的字串會比以 YYYY16 開始的字串優先。 這個值無法與其他 CompareOptions 值搭配使用,而且必須單獨使用。

OrdinalIgnoreCase 268435456

字串比較必須忽略大小寫,然後執行序數比較。 這項技術等於使用非變異文化特性將字串轉換成大寫,然後在結果上執行序數比較。

StringSort 536870912

指示字串比較必須使用字串排序演算法。 在字串排序中,連字號 (-)、所有格符號 (') 以及其他非英數字元的順序會比英數字元優先。

範例

C#
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

另請參閱