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)

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)

'Declaration
Public Function Equals ( _
	value As String, _
	comparisonType As StringComparison _
) As Boolean

Parameters

value
Type: System.String

The string to compare to this instance.

comparisonType
Type: System.StringComparison

One of the enumeration values that specifies how the strings will be compared.

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 by using each possible StringComparison enumeration value.

Class Sample
   Public Shared Sub Main() 
      ' Define a string array with the following three "I" characters: 
      '      U+0069, U+0131, and U+0049.   
      Dim threeIs() As String = { "i", "ı", "I" }
      ' Define Type object representing StringComparison type. 
      Dim scType As Type = GetType(StringComparison)  

      ' Show the current culture (for culture-sensitive string comparisons).
      Console.WriteLine("The current culture is {0}." & vbCrLf, _
                        System.Globalization.CultureInfo.CurrentCulture.Name)

      ' Perform comparisons using each StringComparison member.  
      For Each scName As String In [Enum].GetNames(scType)
         Dim sc As StringComparison = [Enum].Parse(scType, scName)
         Console.WriteLine("Comparisons using {0}:", sc)
         ' Compare each character in character array. 
         For ctr As Integer = 0 To 1
            Dim instanceChar As String = threeIs(ctr)
            For innerCtr As Integer = ctr + 1 To threeIs.GetUpperBound(0)
               Dim otherChar As STring = 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))
            Next
            Console.WriteLine()
         Next 
      Next               
   End Sub 
End Class 
' 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

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, 
                                 strings1(ctr).Equals(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

.NET Framework

Supported in: 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 Silverlight 8.1, Windows Phone Silverlight 8

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.

Show:
© 2014 Microsoft