Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System
String Class
String Methods
Equals Method
 Equals Method (String, StringCompar...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
String.Equals Method (String, StringComparison)

Note: This method is new in the .NET Framework version 2.0.

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)

Visual Basic (Declaration)
Public Function Equals ( _
    value As String, _
    comparisonType As StringComparison _
) As Boolean
Visual Basic (Usage)
Dim instance As String
Dim value As String
Dim comparisonType As StringComparison
Dim returnValue As Boolean

returnValue = instance.Equals(value, comparisonType)
C#
public bool Equals (
    string value,
    StringComparison comparisonType
)
C++
public:
bool Equals (
    String^ value, 
    StringComparison comparisonType
)
J#
public boolean Equals (
    String value, 
    StringComparison comparisonType
)
JScript
public function Equals (
    value : String, 
    comparisonType : StringComparison
) : boolean

Parameters

value

A String object.

comparisonType

One of the System.StringComparison values.

Return Value

true if the value of the value parameter is the same as this string; otherwise, false.
Exception typeCondition

NullReferenceException

This string is a null reference (Nothing in Visual Basic).

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 code example uses three versions of the Equals method to determine whether a String object and a StringBuilder object are equal. The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed.

Visual Basic
' This example demonstrates the 
' System.String.Equals(String, StringComparison) method.

Imports System
Imports System.Threading

Class Sample
    Public Shared Sub Main() 
        Dim intro As String = "Compare three versions of the letter I using different " + "values of StringComparison."
        
        ' Define an array of strings where each element contains a version of the 
        ' letter I. (An array of strings is used so you can easily modify this 
        ' code example to test additional or different combinations of strings.)  
        Dim threeIs(2) As String
        ' LATIN SMALL LETTER I (U+0069)
        threeIs(0) = "i"
        ' LATIN SMALL LETTER DOTLESS I (U+0131)
        threeIs(1) = "ı"
        ' LATIN CAPITAL LETTER I (U+0049)
        threeIs(2) = "I"
        
        Dim unicodeNames As String() = { _
                             "LATIN SMALL LETTER I (U+0069)", _
                             "LATIN SMALL LETTER DOTLESS I (U+0131)", _
                             "LATIN CAPITAL LETTER I (U+0049)" }
        
        Dim scValues As StringComparison() = { _
                        StringComparison.CurrentCulture, _
                        StringComparison.CurrentCultureIgnoreCase, _
                        StringComparison.InvariantCulture, _
                        StringComparison.InvariantCultureIgnoreCase, _
                        StringComparison.Ordinal, _
                        StringComparison.OrdinalIgnoreCase }
        '
        Console.Clear()
        Console.WriteLine(intro)
        
        ' Display the current culture because the culture-specific comparisons
        ' can produce different results with different cultures.
        Console.WriteLine("The current culture is {0}." & vbCrLf, _
                           Thread.CurrentThread.CurrentCulture.Name)
        
        ' Determine whether three versions of the letter I are equal to each other. 
        Dim sc As StringComparison
        For Each sc In  scValues
            Console.WriteLine("StringComparison.{0}:", sc)
            
            ' LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131)
            Test(0, 1, sc, threeIs, unicodeNames)
            
            ' LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049)
            Test(0, 2, sc, threeIs, unicodeNames)
            
            ' LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049)
            Test(1, 2, sc, threeIs, unicodeNames)
            
            Console.WriteLine()
        Next sc
    
    End Sub 'Main
    
    Protected Shared Sub Test(ByVal x As Integer, ByVal y As Integer, _
                              ByVal comparison As StringComparison, _
                              ByVal testI() As String, ByVal testNames() As String) 
        Dim resultFmt As String = "{0} is {1}equal to {2}"
        Dim result As String = "not "
        '
        If testI(x).Equals(testI(y), comparison) Then
            result = ""
        End If
        Console.WriteLine(resultFmt, testNames(x), result, testNames(y))
    
    End Sub 'Test
End Class 'Sample

'
'This code example produces the following results:
'
'Compare three versions of the letter I using different values of StringComparison.
'The current culture is en-US.
'
'StringComparison.CurrentCulture:
'LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)
'
'StringComparison.CurrentCultureIgnoreCase:
'LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)
'
'StringComparison.InvariantCulture:
'LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)
'
'StringComparison.InvariantCultureIgnoreCase:
'LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)
'
'StringComparison.Ordinal:
'LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)
'
'StringComparison.OrdinalIgnoreCase:
'LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)
'
'
C#
// This example demonstrates the 
// System.String.Equals(String, StringComparison) method.

using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
    string intro = "Compare three versions of the letter I using different " + 
                   "values of StringComparison.";

// Define an array of strings where each element contains a version of the 
// letter I. (An array of strings is used so you can easily modify this 
// code example to test additional or different combinations of strings.)  

    string[] threeIs = new string[3];
// LATIN SMALL LETTER I (U+0069)
    threeIs[0] = "\u0069";
// LATIN SMALL LETTER DOTLESS I (U+0131)
    threeIs[1] = "\u0131";
// LATIN CAPITAL LETTER I (U+0049)
    threeIs[2] = "\u0049";

    string[] unicodeNames = 
             {
             "LATIN SMALL LETTER I (U+0069)", 
             "LATIN SMALL LETTER DOTLESS I (U+0131)", 
             "LATIN CAPITAL LETTER I (U+0049)"
             };

    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

//
    Console.Clear();
    Console.WriteLine(intro);

// Display the current culture because the culture-specific comparisons
// can produce different results with different cultures.
    Console.WriteLine("The current culture is {0}.\n", 
                       Thread.CurrentThread.CurrentCulture.Name);

// Determine whether three versions of the letter I are equal to each other. 
    foreach (StringComparison sc in scValues)
        {
        Console.WriteLine("StringComparison.{0}:", sc);

// LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131)
        Test(0, 1, sc, threeIs, unicodeNames);

// LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049)
        Test(0, 2, sc, threeIs, unicodeNames);

// LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049)
        Test(1, 2, sc, threeIs, unicodeNames);

        Console.WriteLine();
        }
    }

    protected static void Test(int x, int y, 
                               StringComparison comparison, 
                               string[] testI, string[] testNames)
    {
    string resultFmt = "{0} is {1}equal to {2}";
    string result = "not ";
//
    if (testI[x].Equals(testI[y], comparison)) 
        result = "";
    Console.WriteLine(resultFmt, testNames[x], result, testNames[y]);
    }
}

/*
This code example produces the following results:

Compare three versions of the letter I using different values of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

StringComparison.CurrentCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

StringComparison.InvariantCulture:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

StringComparison.InvariantCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

StringComparison.Ordinal:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

StringComparison.OrdinalIgnoreCase:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

*/
C++
// This example demonstrates the 
// System.String.Equals(String, StringComparison) method.

using namespace System;
using namespace System::Threading;

void Test(int testStringIndex, int comparisonStringIndex,
     StringComparison comparison, array<String^>^ testI, 
     array<String^>^ testNames)
{
    String^ resultFormat = "{0} is {1}equal to {2}";
    String^ resultString = "not ";

    if (testI[testStringIndex]->Equals(testI[comparisonStringIndex],
        comparison))
    {
        resultString = "";
    }
    Console::WriteLine(resultFormat, testNames[testStringIndex],
        resultString, testNames[comparisonStringIndex]);
}

int main()
{
    String^ introMessage =
        "Compare three versions of the letter I using different " +
        "values of StringComparison.";

    // Define an array of strings where each element contains a version of
    // the letter I. (An array of strings is used so you can easily modify
    // this code example to test additional or different combinations of
    // strings.)  
    array<String^>^ letterVariation = gcnew array<String^>(3);

    // LATIN SMALL LETTER I (U+0069)
    letterVariation[0] = "i";

    // LATIN SMALL LETTER DOTLESS I (U+0131)
    letterVariation[1] = L"\u0131";

    // LATIN CAPITAL LETTER I (U+0049)
    letterVariation[2] = "I";

    array<String^>^ unicodeNames = {
        "LATIN SMALL LETTER I (U+0069)",
        "LATIN SMALL LETTER DOTLESS I (U+0131)",
        "LATIN CAPITAL LETTER I (U+0049)"};

        array<StringComparison>^ comparisonValues = {
            StringComparison::CurrentCulture,
            StringComparison::CurrentCultureIgnoreCase,
            StringComparison::InvariantCulture,
            StringComparison::InvariantCultureIgnoreCase,
            StringComparison::Ordinal,
            StringComparison::OrdinalIgnoreCase};

        Console::Clear();
        Console::WriteLine(introMessage);

        // Display the current culture because the culture-specific comparisons
        // can produce different results with different cultures.
        Console::WriteLine("The current culture is {0}.\n",
            Thread::CurrentThread->CurrentCulture->Name);

        // Determine whether three versions of the letter I are equal to each
        // other. 
        for each (StringComparison stringCmp in comparisonValues)
        {
            Console::WriteLine("StringComparison.{0}:", stringCmp);

            // LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I
            // (U+0131)
            Test(0, 1, stringCmp, letterVariation, unicodeNames);

            // LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049)
            Test(0, 2, stringCmp, letterVariation, unicodeNames);

            // LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I
            // (U+0049)
            Test(1, 2, stringCmp, letterVariation, unicodeNames);

            Console::WriteLine();
        }
}

/*
This code example produces the following results:

Compare three versions of the letter I using different values of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

StringComparison.CurrentCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

StringComparison.InvariantCulture:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

StringComparison.InvariantCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

StringComparison.Ordinal:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is not equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

StringComparison.OrdinalIgnoreCase:
LATIN SMALL LETTER I (U+0069) is not equal to LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is not equal to LATIN CAPITAL LETTER I (U+0049)

*/

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker