Skip to main content
.NET Framework Class Library
StringComparison Enumeration

Specifies the culture, case, and sort rules to be used by certain overloads of the StringCompare and StringEquals methods.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/8d9k4871_en-us_VS_110_3_0_0_0_0%]> _
<[%$TOPIC/8d9k4871_en-us_VS_110_3_0_0_0_1%](True)> _
Public Enumeration StringComparison
[[%$TOPIC/8d9k4871_en-us_VS_110_3_0_1_0_0%]]
[[%$TOPIC/8d9k4871_en-us_VS_110_3_0_1_0_1%](true)]
public enum StringComparison
[[%$TOPIC/8d9k4871_en-us_VS_110_3_0_2_0_0%]]
[[%$TOPIC/8d9k4871_en-us_VS_110_3_0_2_0_1%](true)]
public enum class StringComparison
[<[%$TOPIC/8d9k4871_en-us_VS_110_3_0_3_0_0%]>]
[<[%$TOPIC/8d9k4871_en-us_VS_110_3_0_3_0_1%](true)>]
type StringComparison
public enum StringComparison
Members
Member nameDescription
Supported by the XNA Framework Supported by Portable Class LibraryCurrentCultureCompare strings using culture-sensitive sort rules and the current culture.
Supported by the XNA Framework Supported by Portable Class LibraryCurrentCultureIgnoreCaseCompare strings using culture-sensitive sort rules, the current culture, and ignoring the case of the strings being compared.
Supported by the XNA FrameworkInvariantCultureCompare strings using culture-sensitive sort rules and the invariant culture.
Supported by the XNA FrameworkInvariantCultureIgnoreCaseCompare strings using culture-sensitive sort rules, the invariant culture, and ignoring the case of the strings being compared.
Supported by the XNA Framework Supported by Portable Class LibraryOrdinalCompare strings using ordinal sort rules.
Supported by the XNA Framework Supported by Portable Class LibraryOrdinalIgnoreCaseCompare strings using ordinal sort rules and ignoring the case of the strings being compared.
Remarks

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.

Important noteImportant

When you call a string comparison method such as StringCompare, StringEquals, or StringIndexOf, 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 in the .NET Framework.

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.

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. 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.

For more information about comparisons, see the SystemString class remarks. For more information about culture, see the System.GlobalizationCultureInfo 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 in the .NET Framework.

Examples

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 (Upper Sweden) cultures. Note that the strings "encyclopædia" and "encyclopedia" are considered equivalent in the en-US culture but not in the Sami (Northern Sweden) culture.

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
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
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library