StringComparison StringComparison StringComparison StringComparison Enum

定義

指定 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
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum StringComparison
type StringComparison = 
Public Enum StringComparison
繼承
StringComparisonStringComparisonStringComparisonStringComparison
屬性

欄位

CurrentCulture CurrentCulture CurrentCulture CurrentCulture 0

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

CurrentCultureIgnoreCase CurrentCultureIgnoreCase CurrentCultureIgnoreCase CurrentCultureIgnoreCase 1

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

InvariantCulture InvariantCulture InvariantCulture InvariantCulture 2

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

InvariantCultureIgnoreCase InvariantCultureIgnoreCase InvariantCultureIgnoreCase InvariantCultureIgnoreCase 3

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

Ordinal Ordinal Ordinal Ordinal 4

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

OrdinalIgnoreCase OrdinalIgnoreCase OrdinalIgnoreCase 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.Equals,或String.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.

使用字組排序規則的作業會執行區分文化特性的比較,其中某些非英數字元的 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. 使用字組排序規則和特定文化特性慣例,連字號 ("-") 可能會有很小的權重,讓"coop"和"co-op"會相互並排顯示已排序的清單中,指派給它。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.

使用序數排序規則的作業執行每個數值的值 (Unicode 字碼指標) 為基礎的比較Char字串中。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 +) 時,字串 U + xxxx 前面字串 U + yyyy 如果 xxxx 的值加上數字小於 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.

適用於

另請參閱