CompareOptions 枚举

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

此枚举有一个 FlagsAttribute 属性,允许其成员值按位组合。

**命名空间:**System.Globalization
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<FlagsAttribute> _
Public Enumeration CompareOptions
用法
Dim instance As CompareOptions
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[FlagsAttribute] 
public enum CompareOptions
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[FlagsAttribute] 
public enum class CompareOptions
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute FlagsAttribute() */ 
public enum CompareOptions
SerializableAttribute 
ComVisibleAttribute(true) 
FlagsAttribute 
public enum CompareOptions

成员

  成员名称 说明
由 .NET Compact Framework 支持 IgnoreCase 指示字符串比较必须忽略大小写。 
由 .NET Compact Framework 支持 IgnoreKanaType 指示字符串比较必须忽略 Kana 类型。假名类型是指日语平假名和片假名字符,它们表示日语中的语音。平假名用于表示日语自有的短语和字词,而片假名用于表示从其他语言借用的字词,如“computer”或“Internet”。语音既可以用平假名也可以用片假名表示。如果选择该值,则认为一个语音的平假名字符等于同一语音的片假名字符。 
由 .NET Compact Framework 支持 IgnoreNonSpace 指示字符串比较必须忽略不占空间的组合字符,比如音调符号。Unicode 标准将组合字符定义为与基字符组合起来产生新字符的字符。不占空间的组合字符在呈现时其本身不占用空间位置。有关不占空间的组合字符的更多信息,请参见 http://www.unicode.org 中的“The Unicode Standard”(Unicode 标准)。 
由 .NET Compact Framework 支持 IgnoreSymbols 指示字符串比较必须忽略符号,如空白字符、标点符号、货币符号、百分号、数学符号、“&”符等等。 
由 .NET Compact Framework 支持 IgnoreWidth 指示字符串比较必须忽略字符宽度。例如,日语片假名字符可以写为全角或半角形式;如果选择该值,则认为写为全角形式的片假名字符等于写为半角形式的同一字符。 
由 .NET Compact Framework 支持 None 指定字符串比较的默认选项设置。 
由 .NET Compact Framework 支持 Ordinal 指示必须使用每个字符的 Unicode 值进行字符串比较;这是一种快速比较方式,但是不区分区域性。如果 xxxx 小于 yyyy,则以“U+xxxx”开头的字符串位于以“U+yyyy”开头的字符串之前。此标志不能和其他标志组合使用,必须单独使用。 
由 .NET Compact Framework 支持 OrdinalIgnoreCase 指示字符串比较必须忽略大小写,然后执行序号比较。这等效于使用固定区域性将字符串转换成大写,然后对结果执行序号比较。 
由 .NET Compact Framework 支持 StringSort 指示字符串比较必须使用字符串排序算法;在这种算法中,连字符和省字号以及其他非字母数字字符均位于字母数字符号前。 

备注

这些选项指明是否区分大小写以及是否忽略字符类型。

.NET Framework 使用了三种不同的排序方式:单词排序、字符串排序和序号排序。单词排序执行区分区域性的字符串比较。某些非字母数字字符可能被指定了特殊权重;例如,可能为连字符(“-”)指定了很小的权重,这样,“coop”和“co-op”会在排序后的列表中相邻出现。字符串排序与单词排序相似(唯一区别是字符串排序中不存在特殊情况);因此,所有非字母数字字符排在所有字母数字字符之前。而序号排序基于字符串中各元素的 Unicode 值对字符串进行比较。

StringSort 值只能用于 CompareInfo.CompareCompareInfo.GetSortKey。如果 StringSort 值用于 CompareInfo.IsPrefixCompareInfo.IsSuffixCompareInfo.IndexOfCompareInfo.LastIndexOf,则会引发 ArgumentException

示例

下面的代码示例显示有 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
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

*/
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
*/
import System.* ;
import System.Collections.* ;
import System.Globalization.* ;

public class SamplesCompareOptions
{
    private class MyStringComparer implements 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;
        } //MyStringComparer

        // 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 =(String) a;
            String sb = (String)b;
            if (sa  != null && sb  != null) {
                return myComp.Compare(sa, sb, myOptions);
            }
            throw new ArgumentException("a and b should be strings.");
        } //Compare 
    } //MyStringComparer
   
    public static void main(String[] args)
    {
        // Creates and initializes an array of strings to sort.
        String myArr[] = new String[]{"cant", "bill's", "coop", "cannot", 
            "billet", "can't", "con", "bills", "co-op"};
        Console.WriteLine("\nInitially,");
            SamplesCompareOptions mySamplesCompareOptions = 
                new SamplesCompareOptions();    
        
        for(int i=0 ; i < myArr.length ;i++) {
            String myStr = myArr[i];
            Console.WriteLine(myStr);
        }
        // Creates and initializes a Comparer to use.
        //CultureInfo myCI = new CultureInfo( "en-US", false );
        MyStringComparer myComp = mySamplesCompareOptions.new MyStringComparer(
            CompareInfo.GetCompareInfo("en-US"), CompareOptions.None);
            
        // Sorts the array without StringSort.
        Array.Sort(myArr, myComp);
        Console.WriteLine("\nAfter sorting without CompareOptions.StringSort:");
        
        for(int i=0; i< myArr.length ;i++) {
            String myStr = myArr[i];
            Console.WriteLine(myStr);
        }
            
        // Sorts the array with StringSort.
        myComp = mySamplesCompareOptions.new MyStringComparer(
            CompareInfo.GetCompareInfo("en-US"), CompareOptions.StringSort);
        Array.Sort(myArr, myComp);
        Console.WriteLine("\nAfter sorting with CompareOptions.StringSort:");
        
        for(int i=0; i< myArr.length ;i++) {
            String myStr = myArr[i];
            Console.WriteLine(myStr);
        }
    } //main 
} //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
*/

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

System.Globalization 命名空间

其他资源

基本字符串操作