Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Equals Method (String, StringComparison)

Updated: May 2009

Determines whether this string and a specified String object 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 bool Equals(
	string value,
	StringComparison comparisonType
)

Parameters

value
Type: System.String

A string to compare.

comparisonType
Type: System.StringComparison

A value that defines the type of comparison.

Return Value

Type: System.Boolean
true if the value of the value parameter is the same as this string; 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 creates a string array that consists of an uppercase "I", a lowercase "i", and a dotless "ı". It then calls the Equals(String, StringComparison) method to compare them using each possible StringComparison enumeration value.

using System;

class Sample 
{
   public static void Main() 
   {
      // Define a string array with the following three "I" characters:
      //      U+0069, U+0131, and U+0049.  
      string[] threeIs = { "i", "ı", "I" };
      // Define Type object representing StringComparison type.
      Type scType = typeof(StringComparison);  

      // Show the current culture (for culture-sensitive string comparisons).
      Console.WriteLine("The current culture is {0}.\n", 
                        System.Globalization.CultureInfo.CurrentCulture.Name);

      // Perform comparisons using each StringComparison member.  
      foreach (string scName in Enum.GetNames(scType))
      {
         StringComparison sc = (StringComparison) Enum.Parse(scType, scName);
         Console.WriteLine("Comparisons using {0}:", sc);
         // Compare each character in character array. 
         for (int ctr = 0; ctr <= 1; ctr++)
         {
            string instanceChar = threeIs[ctr];
            for (int innerCtr = ctr + 1; innerCtr <= threeIs.GetUpperBound(0); innerCtr++)
            {
               string otherChar = threeIs[innerCtr];
               Console.WriteLine("{0} (U+{1}) = {2} (U+{3}): {4}", 
                                 instanceChar, Convert.ToInt16(Char.Parse(instanceChar)).ToString("X4"), 
                                 otherChar, Convert.ToInt16(Char.Parse(otherChar)).ToString("X4"), 
                                 instanceChar.Equals(otherChar, sc));
            }
            Console.WriteLine();
         }
      }   
   }
}
// The example displays the following output: 
//       The current culture is en-US. 
//        
//       Comparisons using CurrentCulture: 
//       i (U+0069) = i (U+0131): False 
//       i (U+0069) = I (U+0049): False 
//        
//       i (U+0131) = I (U+0049): False 
//        
//       Comparisons using CurrentCultureIgnoreCase: 
//       i (U+0069) = i (U+0131): False 
//       i (U+0069) = I (U+0049): True 
//        
//       i (U+0131) = I (U+0049): False 
//        
//       Comparisons using InvariantCulture: 
//       i (U+0069) = i (U+0131): False 
//       i (U+0069) = I (U+0049): False 
//        
//       i (U+0131) = I (U+0049): False 
//        
//       Comparisons using InvariantCultureIgnoreCase: 
//       i (U+0069) = i (U+0131): False 
//       i (U+0069) = I (U+0049): True 
//        
//       i (U+0131) = I (U+0049): False 
//        
//       Comparisons using Ordinal: 
//       i (U+0069) = i (U+0131): False 
//       i (U+0069) = I (U+0049): False 
//        
//       i (U+0131) = I (U+0049): False 
//        
//       Comparisons using OrdinalIgnoreCase: 
//       i (U+0069) = i (U+0131): False 
//       i (U+0069) = I (U+0049): True 
//        
//       i (U+0131) = I (U+0049): False

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

May 2009

Replaced the example.

Customer feedback.

February 2009

Updated exception information.

Content bug fix.

Community Additions

Show:
© 2015 Microsoft