CompareOptions 열거형

정의

CompareInfo와 함께 사용할 문자열 비교 옵션을 정의합니다.

이 열거형은 멤버 값의 비트 조합을 지원합니다.

public enum class CompareOptions
[System.Flags]
public enum CompareOptions
[System.Flags]
[System.Serializable]
public enum CompareOptions
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum CompareOptions
[<System.Flags>]
type CompareOptions = 
[<System.Flags>]
[<System.Serializable>]
type CompareOptions = 
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CompareOptions = 
Public Enum CompareOptions
상속
CompareOptions
특성

필드

IgnoreCase 1

이 문자열 비교 옵션은 대/소문자를 무시함을 나타냅니다.

IgnoreKanaType 8

이 문자열 비교 옵션은 일본어 가나 형식을 무시함을 나타냅니다. 가나 형식은 일본어의 발성음을 표현하는 히라가나 문자와 가타카나 문자를 나타냅니다. 히라가나는 일본 고유의 어구과 단어를 표현하는 데 사용되고, 가타카나는 "컴퓨터"나 "인터넷" 등과 같은 외래어를 표현하는 데 사용됩니다. 발성음은 히라가나와 가타카나 모두로 표현할 수 있습니다. 이 값이 선택되어 있으면 하나의 발성음에 대해 히라가나 문자와 가타카나 문자가 같은 것으로 간주됩니다.

IgnoreNonSpace 2

분음 부호와 같이 공백 없는 조합 문자를 무시하는 문자열 비교를 나타냅니다. 유니코드 표준(Unicode Standard)에서 조합 문자는 기본 문자와 조합하여 새 문자를 만들어내는 문자로 정의됩니다. 간격이 없는 조합 문자는 렌더링될 때 스스로 공간을 차지하지 않습니다.

IgnoreSymbols 4

이 문자열 비교 옵션은 공백 문자, 문장 부호, 통화 기호, 백분율 기호, 수학 기호, 앰퍼샌드 등의 기호를 무시함을 나타냅니다.

IgnoreWidth 16

이 문자열 비교 옵션은 문자 너비를 무시함을 나타냅니다. 예를 들어 일본어의 가타카나 문자는 전자나 반자로 쓸 수 있는데, 이 값이 선택되어 있으면 전자로 쓰여진 가타카나 문자와 반자로 쓰여진 가타카나 문자가 같은 것으로 간주됩니다.

None 0

문자열 비교를 위한 기본 옵션 설정을 나타냅니다.

Ordinal 1073741824

문자열 비교가 문자열의 후속 유니코드 UTF-16 인코딩 값을 사용해야 함을 나타냅니다(코드 단위별 코드 단위 비교). 이 값을 사용하면 문자열을 빠르게 비교할 수 있지만 문화권을 구분할 수는 없습니다. XXXX16이 YYYY16보다 작은 경우 코드 단위 XXXX16으로 시작하는 문자열이 YYYY16으로 시작하는 문자열 앞에 옵니다. 이 값은 다른 CompareOptions 값과 함께 사용할 수 없으며 단독으로 사용해야 합니다.

OrdinalIgnoreCase 268435456

문자열 비교에서는 대/소문자를 무시하고 서수 비교를 수행해야 합니다. 이 기술은 고정 문화권을 사용하여 문자열을 대문자로 변환한 다음 해당 결과에 대해 서수 비교를 수행하는 것과 같습니다.

StringSort 536870912

이 문자열 비교 옵션은 문자열 정렬 알고리즘을 사용해야 함을 나타냅니다. 문자열 정렬에서 하이픈, 아포스트로피, 비영숫자 기호 등이 영숫자 문자 앞에 옵니다.

예제

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

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

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

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

      if ( a == nullptr )
            return  -1;

      if ( b == nullptr )
            return 1;

      String^ sa = dynamic_cast<String^>(a);
      String^ sb = dynamic_cast<String^>(b);
      if ( sa != nullptr && sb != nullptr )
            return myComp->Compare( sa, sb, myOptions );

      throw gcnew ArgumentException( "a and b should be strings." );
   }
};

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

   
   // Creates and initializes a Comparer to use.
   //CultureInfo* myCI = new CultureInfo(S"en-US", false);
   MyStringComparer^ myComp = gcnew MyStringComparer( CompareInfo::GetCompareInfo( "en-US" ),CompareOptions::None );
   
   // Sorts the array without StringSort.
   Array::Sort( myArr, myComp );
   Console::WriteLine( "\nAfter sorting without CompareOptions::StringSort:" );
   myEnum = myArr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      String^ myStr = safe_cast<String^>(myEnum->Current);
      Console::WriteLine( myStr );
   }

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

*/
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
      
      ' 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


   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

End Class


'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 설명을 참조하세요.

적용 대상

추가 정보