Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

String.Equals Method (String, String, StringComparison)

Determines whether two specified String objects have the same value. A parameter specifies the culture, case, and sort rules used in the comparison.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static bool Equals(
	string a,
	string b,
	StringComparison comparisonType
)

Parameters

a
Type: System.String

The first string to compare, or null.

b
Type: System.String

The second string to compare, or null.

comparisonType
Type: System.StringComparison

One of the enumeration values that specifies the rules for the comparison.

Return Value

Type: System.Boolean
true if the value of the a parameter is equal to the value of the b parameter; otherwise, false.
ExceptionCondition
ArgumentException

comparisonType is not a StringComparison value.

The comparisonType parameter indicates whether the comparison should use the current or invariant culture, honor or ignore the case of the two strings being compared, or use word or ordinal sort rules.

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.

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

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

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

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.