StringComparison 列舉

定義

指定 Compare(String, String)Equals(Object) 方法的特定多載所要使用的文化特性、大小寫和排序規則。Specifies the culture, case, and sort rules to be used by certain overloads of the Compare(String, String) and Equals(Object) methods.

public enum class StringComparison
public enum StringComparison
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum StringComparison
type StringComparison = 
Public Enum StringComparison
繼承
StringComparison
屬性

欄位

CurrentCulture 0

比較字串時,使用區分文化特性的排序規則和目前的文化特性。Compare strings using culture-sensitive sort rules and the current culture.

CurrentCultureIgnoreCase 1

比較字串時,使用區分文化特性的排序規則和目前的文化特性,並且忽略要比較之字串的大小寫。Compare strings using culture-sensitive sort rules, the current culture, and ignoring the case of the strings being compared.

InvariantCulture 2

比較字串時,使用區分文化特性的排序規則,並且不因文化特性而異。Compare strings using culture-sensitive sort rules and the invariant culture.

InvariantCultureIgnoreCase 3

比較字串時,使用區分文化特性的排序規則、不因文化特性而異,並且忽略要比較之字串的大小寫。Compare strings using culture-sensitive sort rules, the invariant culture, and ignoring the case of the strings being compared.

Ordinal 4

比較字串時,使用序數 (二進位) 排序規則。Compare strings using ordinal (binary) sort rules.

OrdinalIgnoreCase 5

比較字串時,使用序數 (二進位) 排序規則,並且忽略要比較之字串的大小寫。Compare strings using ordinal (binary) sort rules and ignoring the case of the strings being compared.

範例

下列範例將使用 StringComparison 列舉型別的每個成員來比較四組單字。The following example compares four sets of words by using each member of the StringComparison enumeration. 比較時使用了英文 (美國) 和北薩米文 (瑞典) 文化特性的慣例。The comparisons use the conventions of the English (United States) and Sami, Northern (Sweden) cultures. 請注意,字串 "encyclopædia" 和 "encyclopedia" 在 en-US 文化特性下會被視為相等,但在北薩米文 (瑞典) 文化特性下則不然。Note that the strings "encyclopædia" and "encyclopedia" are considered equivalent in the en-US culture but not in the Sami, Northern (Sweden) culture.

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      String[] cultureNames = { "en-US", "se-SE" };
      String[] strings1 = { "case",  "encyclopædia",  
                            "encyclopædia", "Archæology" };
      String[] strings2 = { "Case", "encyclopaedia", 
                            "encyclopedia" , "ARCHÆOLOGY" };
      StringComparison[] comparisons = (StringComparison[]) Enum.GetValues(typeof(StringComparison));
      
      foreach (var cultureName in cultureNames) {
         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
         Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name);
         for (int ctr = 0; ctr <= strings1.GetUpperBound(0); ctr++) {
            foreach (var comparison in comparisons) 
               Console.WriteLine("   {0} = {1} ({2}): {3}", strings1[ctr],
                                 strings2[ctr], comparison, 
                                 String.Equals(strings1[ctr], strings2[ctr], comparison));

            Console.WriteLine();         
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//    Current Culture: en-US
//       case = Case (CurrentCulture): False
//       case = Case (CurrentCultureIgnoreCase): True
//       case = Case (InvariantCulture): False
//       case = Case (InvariantCultureIgnoreCase): True
//       case = Case (Ordinal): False
//       case = Case (OrdinalIgnoreCase): True
//    
//       encyclopædia = encyclopaedia (CurrentCulture): True
//       encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): True
//       encyclopædia = encyclopaedia (InvariantCulture): True
//       encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
//       encyclopædia = encyclopaedia (Ordinal): False
//       encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
//    
//       encyclopædia = encyclopedia (CurrentCulture): False
//       encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
//       encyclopædia = encyclopedia (InvariantCulture): False
//       encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
//       encyclopædia = encyclopedia (Ordinal): False
//       encyclopædia = encyclopedia (OrdinalIgnoreCase): False
//    
//       Archæology = ARCHÆOLOGY (CurrentCulture): False
//       Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
//       Archæology = ARCHÆOLOGY (InvariantCulture): False
//       Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
//       Archæology = ARCHÆOLOGY (Ordinal): False
//       Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True
//    
//    
//    Current Culture: se-SE
//       case = Case (CurrentCulture): False
//       case = Case (CurrentCultureIgnoreCase): True
//       case = Case (InvariantCulture): False
//       case = Case (InvariantCultureIgnoreCase): True
//       case = Case (Ordinal): False
//       case = Case (OrdinalIgnoreCase): True
//    
//       encyclopædia = encyclopaedia (CurrentCulture): False
//       encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): False
//       encyclopædia = encyclopaedia (InvariantCulture): True
//       encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
//       encyclopædia = encyclopaedia (Ordinal): False
//       encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
//    
//       encyclopædia = encyclopedia (CurrentCulture): False
//       encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
//       encyclopædia = encyclopedia (InvariantCulture): False
//       encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
//       encyclopædia = encyclopedia (Ordinal): False
//       encyclopædia = encyclopedia (OrdinalIgnoreCase): False
//    
//       Archæology = ARCHÆOLOGY (CurrentCulture): False
//       Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
//       Archæology = ARCHÆOLOGY (InvariantCulture): False
//       Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
//       Archæology = ARCHÆOLOGY (Ordinal): False
//       Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "se-SE" }
      Dim strings1() As String = { "case",  "encyclopædia",  
                                   "encyclopædia", "Archæology" }
      Dim strings2() As String = { "Case", "encyclopaedia", 
                                   "encyclopedia" , "ARCHÆOLOGY" }
      Dim comparisons() As StringComparison = CType([Enum].GetValues(GetType(StringComparison)),
                                           StringComparison())
      
      For Each cultureName In cultureNames
         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName)
         Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name)
         For ctr As Integer = 0 To strings1.GetUpperBound(0)
            For Each comparison In comparisons
               Console.WriteLine("   {0} = {1} ({2}): {3}", strings1(ctr),
                                 strings2(ctr), comparison, 
                                 String.Equals(strings1(ctr), strings2(ctr), comparison))
            Next
            Console.WriteLine()         
         Next
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output:
'    Current Culture: en-US
'       case = Case (CurrentCulture): False
'       case = Case (CurrentCultureIgnoreCase): True
'       case = Case (InvariantCulture): False
'       case = Case (InvariantCultureIgnoreCase): True
'       case = Case (Ordinal): False
'       case = Case (OrdinalIgnoreCase): True
'    
'       encyclopædia = encyclopaedia (CurrentCulture): True
'       encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): True
'       encyclopædia = encyclopaedia (InvariantCulture): True
'       encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
'       encyclopædia = encyclopaedia (Ordinal): False
'       encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
'    
'       encyclopædia = encyclopedia (CurrentCulture): False
'       encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
'       encyclopædia = encyclopedia (InvariantCulture): False
'       encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
'       encyclopædia = encyclopedia (Ordinal): False
'       encyclopædia = encyclopedia (OrdinalIgnoreCase): False
'    
'       Archæology = ARCHÆOLOGY (CurrentCulture): False
'       Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
'       Archæology = ARCHÆOLOGY (InvariantCulture): False
'       Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
'       Archæology = ARCHÆOLOGY (Ordinal): False
'       Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True
'    
'    
'    Current Culture: se-SE
'       case = Case (CurrentCulture): False
'       case = Case (CurrentCultureIgnoreCase): True
'       case = Case (InvariantCulture): False
'       case = Case (InvariantCultureIgnoreCase): True
'       case = Case (Ordinal): False
'       case = Case (OrdinalIgnoreCase): True
'    
'       encyclopædia = encyclopaedia (CurrentCulture): False
'       encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): False
'       encyclopædia = encyclopaedia (InvariantCulture): True
'       encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
'       encyclopædia = encyclopaedia (Ordinal): False
'       encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
'    
'       encyclopædia = encyclopedia (CurrentCulture): False
'       encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
'       encyclopædia = encyclopedia (InvariantCulture): False
'       encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
'       encyclopædia = encyclopedia (Ordinal): False
'       encyclopædia = encyclopedia (OrdinalIgnoreCase): False
'    
'       Archæology = ARCHÆOLOGY (CurrentCulture): False
'       Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
'       Archæology = ARCHÆOLOGY (InvariantCulture): False
'       Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
'       Archæology = ARCHÆOLOGY (Ordinal): False
'       Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True

備註

StringComparison 列舉是用來指定字串比較是否應該使用目前的文化特性或不因文化特性而異、word 或序數排序規則,並且區分大小寫或不區分大小寫。The StringComparison enumeration is used to specify whether a string comparison should use the current culture or the invariant culture, word or ordinal sort rules, and be case-sensitive or case-insensitive.

重要

當您呼叫字串比較方法(例如 String.CompareString.EqualsString.IndexOf)時,您應該一律呼叫包含類型 StringComparison 參數的多載,以便指定方法所執行的比較類型。When you call a string comparison method such as String.Compare, String.Equals, or String.IndexOf, you should always call an overload that includes a parameter of type StringComparison so that you can specify the type of comparison that the method performs. 如需詳細資訊,請參閱使用字串的最佳做法For more information, see Best Practices for Using Strings.

使用 word 排序規則的作業會執行區分文化特性的比較,其中某些非英數位元的 Unicode 字元可能會被指派特殊權數。An operation that uses word sort rules performs a culture-sensitive comparison wherein certain nonalphanumeric Unicode characters might have special weights assigned to them. 使用文字排序規則和特定文化特性的慣例,連字號("-")可能會指派極小的權數,讓「合作」和「共同作業」在排序清單中彼此旁顯示。Using word sort rules and the conventions of a specific culture, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list.

注意

.NET Core 僅在 Linux 和 macOS 系統上執行: C 和 Posix 文化特性的定序行為一律區分大小寫,因為這些文化特性不會使用預期的 Unicode 定序順序。.NET Core running on Linux and macOS systems only: The collation behavior for the C and Posix cultures is always case-sensitive because these cultures do not use the expected Unicode collation order. 建議您使用 C 或 Posix 以外的文化特性來執行區分文化特性、不區分大小寫的排序作業。We recommend that you use a culture other than C or Posix for performing culture-sensitive, case-insensitive sorting operations.

使用序數排序規則的作業會根據字串中每個 Char 的數值(Unicode 程式碼點)來執行比較。An operation that uses ordinal sort rules performs a comparison based on the numeric value (Unicode code point) of each Char in the string. 序數比較快速,但不區分文化特性。An ordinal comparison is fast but culture-insensitive. 當您使用序數排序規則來排序以 Unicode 字元(U +)開頭的字串時,如果 xxxx 的值的數值小於 yyyy,則字串 U + xxxx 會在字串 U + yyyy 前面。When you use ordinal sort rules to sort strings that start with Unicode characters (U+), the string U+xxxx comes before the string U+yyyy if the value of xxxx is numerically less than yyyy.

如需比較的詳細資訊,請參閱 System.String 類別的備註。For more information about comparisons, see the System.String class remarks. 如需文化特性的詳細資訊,請參閱 System.Globalization.CultureInfo 類別的備註。For more information about culture, see the System.Globalization.CultureInfo class remarks. 如需何時使用序數或區分文化特性的比較規則或不因文化特性而異之規則的指導方針,請參閱使用字串的最佳作法For guidelines on when to use ordinal or culture-sensitive comparison rules or the rules of the invariant culture, see Best Practices for Using Strings. 如需一組文字檔,其中包含用於 Windows 作業系統排序和比較作業的字元權數資訊,請參閱排序權數資料表For a set of text files that contain information on the character weights used in sorting and comparison operations for Windows operating systems, see Sorting Weight Tables. 如需適用于 Linux 和 macOS 的排序權數資料表,請參閱預設 Unicode 定序元素表For the sort weight table for Linux and macOS, see the Default Unicode Collation Element Table.

適用於

另請參閱