CompareOptions 列挙体

CompareInfo と共に使用する文字列比較オプションを定義します。

この列挙体には、メンバ値をビットごとに演算するための FlagsAttribute 属性が含まれています。

<Flags>
<Serializable>
Public Enum CompareOptions
[C#]
[Flags]
[Serializable]
public enum CompareOptions
[C++]
[Flags]
[Serializable]
__value public enum CompareOptions
[JScript]
public
   Flags
 Serializable
enum CompareOptions

解説

これらのオプションは、大文字と小文字を区別するかどうか、または文字の型を無視するかどうかを示します。

.NET Framework は、単語での並べ替え、文字列での並べ替え、序数での並べ替えの異なる 3 種類の並べ替えの方法を使用します。単語での並べ替えでは、カルチャを考慮した文字列比較が実行されます。英数字以外の特定の文字に特殊な重み付けが行われる場合があります。たとえば、ハイフン ("-") に非常に低い重み付けが行われ、並べ替えられた一覧に "coop" と "co-op" が並んで現れる場合があります。文字列での並べ替えは、単語での並べ替えと似ていますが、特殊な場合が存在しません。このため、英数字以外のすべての記号がどの英数字よりも前に来ます。序数での並べ替えでは、文字列の各要素の Unicode 値を元に文字列が比較されます。

StringSort 値は、 CompareInfo.CompareCompareInfo.GetSortKey だけで使用できます。 StringSort 値を CompareInfo.IsPrefixCompareInfo.IsSuffixCompareInfo.IndexOf 、または CompareInfo.LastIndexOf で使用すると、 ArgumentException がスローされます。

メンバ

メンバ名 説明
IgnoreCase

.NET Compact Framework でもサポート。

文字列比較で大文字と小文字の区別を無視することを示します。 1
IgnoreKanaType

.NET Compact Framework でもサポート。

文字列比較でカナ型を無視することを示します。カナ型とは、日本語の発音を表すひらがなとカタカナの文字を指します。ひらがなは、本来の日本語の表現と単語に使用し、カタカナは "コンピュータ" または "インターネット" などの外来語に使用します。発音は、ひらがなとカタカナのどちらでも表現できます。この値が選択されている場合、ある発音を示すひらがなは、同じ発音を示すカタカナと同一であると見なされます。 8
IgnoreNonSpace

.NET Compact Framework でもサポート。

文字列比較で、発音区別符など、非スペーシング組み合わせ文字を無視するように指定します。Unicode 標準は、基本文字を組み合わせて生成される新しい文字を組み合わせ文字として定義しています。非スペーシング組み合わせ文字は、表示されるときに文字間隔用の領域は確保しません。非スペーシング組み合わせ文字の詳細については、http://www.unicode.org の「The Unicode Standard」を参照してください。 2
IgnoreSymbols

.NET Compact Framework でもサポート。

文字列比較において、空白文字、句読点、通貨記号、パーセント記号、算術記号、アンパサンドなどの記号を無視することを示します。 4
IgnoreWidth

.NET Compact Framework でもサポート。

文字列比較において、半角と全角の区別を無視することを示します。たとえば、日本語のカタカナ文字は、全角または半角で記述できます。この値を選択した場合、全角で記述されたカタカナ文字は、半角で記述されたカタカナ文字と同一であると見なされます。 16
None

.NET Compact Framework でもサポート。

文字列比較の既定のオプション設定を示します。 0
Ordinal

.NET Compact Framework でもサポート。

各文字の Unicode 値を使用して、文字列を比較することを示します。この比較は処理速度は速いですが、カルチャに応じた処理は行いません。xxxx が yyyy よりも小さい場合、"U+xxxx" で始まる文字列は "U+yyyy" で始まる文字列よりも前になります。このフラグを他のフラグと組み合わせることはできません。このフラグは、単独で使用してください。 1073741824
StringSort

.NET Compact Framework でもサポート。

文字列の比較時に、ハイフン、アポストロフィ、およびその他の英数字以外の記号が英数字よりも前になる文字列並べ替えアルゴリズムを使用することを示します。 536870912

使用例

[Visual Basic, C#, C++] StringSort を指定した並べ替えと StringSort なしでの並べ替えがどのように異なるかを次のコード例で示します。

 
Imports System
Imports System.Collections
Imports System.Globalization

Public Class SamplesCompareOptions

   Private Class MyStringComparer
      Implements IComparer

      Private myComp As CompareInfo
      Private myOptions As CompareOptions = CompareOptions.None
      
      ' Constructs a comparer using the specified CompareOptions.
      Public Sub New(cmpi As CompareInfo, options As CompareOptions)
         myComp = cmpi
         Me.myOptions = options
      End Sub 'New
      
      ' Compares strings with the CompareOptions specified in the constructor.
      Public Function Compare(a As [Object], b As [Object]) As Integer Implements IComparer.Compare
         If a = b Then
            Return 0
         End If
         If a Is Nothing Then
            Return - 1
         End If
         If b Is Nothing Then
            Return 1
         End If 

         Dim sa As [String] = a
         Dim sb As [String] = b
         If Not (sa Is Nothing) And Not (sb Is Nothing) Then
            Return myComp.Compare(sa, sb, myOptions)
         End If
         Throw New ArgumentException("a and b should be strings.")

      End Function 'Compare 

   End Class 'MyStringComparer


   Public Shared Sub Main()
      
      ' Creates and initializes an array of strings to sort.
      Dim myArr() As [String] = {"cant", "bill's", "coop", "cannot", "billet", "can't", "con", "bills", "co-op"}
      Console.WriteLine()
      Console.WriteLine("Initially,")
      Dim myStr As [String]
      For Each myStr In  myArr
         Console.WriteLine(myStr)
      Next myStr 

      ' Creates and initializes a Comparer to use.
      'CultureInfo myCI = new CultureInfo( "en-US", false );
      Dim myComp As New MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.None)
      
      ' Sorts the array without StringSort.
      Array.Sort(myArr, myComp)
      Console.WriteLine()
      Console.WriteLine("After sorting without CompareOptions.StringSort:")
      For Each myStr In  myArr
         Console.WriteLine(myStr)
      Next myStr 

      ' Sorts the array with StringSort.
      myComp = New MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.StringSort)
      Array.Sort(myArr, myComp)
      Console.WriteLine()
      Console.WriteLine("After sorting with CompareOptions.StringSort:")
      For Each myStr In  myArr
         Console.WriteLine(myStr)
      Next myStr 

   End Sub 'Main

End Class 'SamplesCompareOptions 


'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


[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

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

// __gc public class SamplesCompareOptions {
__gc class MyStringComparer : public IComparer
{

   // Constructs a comparer using the specified CompareOptions.
public:
   CompareInfo * myComp;
   CompareOptions myOptions;

   MyStringComparer(CompareInfo* cmpi, CompareOptions options) : myComp(cmpi), myOptions(options)
   {
   }

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

      String* sa = dynamic_cast<String*>(a);
      String* sb = dynamic_cast<String*>(b);
      if (sa != 0 && sb != 0)
         return myComp->Compare(sa, sb, myOptions);
      throw new ArgumentException(S"a and b should be strings.");

   }
};

int main()
{
   // Creates and initializes an array of strings to sort.
   String* myArr[] = { S"cant", S"bill's", S"coop", S"cannot", S"billet", S"can't", S"con", S"bills", S"co-op" };
   Console::WriteLine(S"\nInitially, ");
   IEnumerator* myEnum = myArr->GetEnumerator();
   while (myEnum->MoveNext())
   {
      String* myStr = __try_cast<String*>(myEnum->Current);

      Console::WriteLine(myStr);
   }

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

   // Sorts the array without StringSort.

   Array::Sort(myArr, myComp);
   Console::WriteLine(S"\nAfter sorting without CompareOptions::StringSort:");
   myEnum = myArr->GetEnumerator();
   while (myEnum->MoveNext())
   {
      String* myStr = __try_cast<String*>(myEnum->Current);
      Console::WriteLine(myStr);
   }

   // Sorts the array with StringSort.
   myComp = new MyStringComparer(CompareInfo::GetCompareInfo(S"en-US"), CompareOptions::StringSort);
   Array::Sort(myArr, myComp);
   Console::WriteLine(S"\nAfter sorting with CompareOptions::StringSort:");
   myEnum = myArr->GetEnumerator();
   while (myEnum->MoveNext())
   {
      String* myStr = __try_cast<String*>(myEnum->Current);
      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
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Globalization

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

System.Globalization 名前空間 | 基本的な文字列操作