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, String, StringComparison)

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

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
)
public static boolean Equals (
	String a, 
	String b, 
	StringComparison comparisonType
)
public static function Equals (
	a : String, 
	b : String, 
	comparisonType : StringComparison
) : boolean

Parameters

a

A String object or a null reference (Nothing in Visual Basic).

b

A String object or a null reference (Nothing in Visual Basic).

comparisonType

One of the System.StringComparison values.

Return Value

true if the value of the a parameter is equal to the value of the b parameter; otherwise, false.

Exception typeCondition

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.

// Sample for String.Equals(Object)
//            String.Equals(String)
//            String.Equals(String, String)
using System;
using System.Text;

class Sample {
    public static void Main() {
    StringBuilder sb = new StringBuilder("abcd");
    String      str1 = "abcd";
    String      str2 = null;
    Object	o2   = null;

    Console.WriteLine();
    Console.WriteLine(" *  The value of String str1 is '{0}'.", str1);
    Console.WriteLine(" *  The value of StringBuilder sb is '{0}'.", sb.ToString());

    Console.WriteLine();
    Console.WriteLine("1a) String.Equals(Object). Object is a StringBuilder, not a String.");
    Console.WriteLine("    Is str1 equal to sb?: {0}", str1.Equals(sb));

    Console.WriteLine();
    Console.WriteLine("1b) String.Equals(Object). Object is a String.");
    str2 = sb.ToString();
    o2   = str2;
    Console.WriteLine(" *  The value of Object o2 is '{0}'.", o2);
    Console.WriteLine("    Is str1 equal to o2?: {0}", str1.Equals(o2));

    Console.WriteLine();
    Console.WriteLine(" 2) String.Equals(String)");
    Console.WriteLine(" *  The value of String str2 is '{0}'.", str2);
    Console.WriteLine("    Is str1 equal to str2?: {0}", str1.Equals(str2));

    Console.WriteLine();
    Console.WriteLine(" 3) String.Equals(String, String)");
    Console.WriteLine("    Is str1 equal to str2?: {0}", String.Equals(str1, str2));
    }
}
/*
This example produces the following results:

 *  The value of String str1 is 'abcd'.
 *  The value of StringBuilder sb is 'abcd'.

1a) String.Equals(Object). Object is a StringBuilder, not a String.
    Is str1 equal to sb?: False

1b) String.Equals(Object). Object is a String.
 *  The value of Object o2 is 'abcd'.
    Is str1 equal to o2?: True

 2) String.Equals(String)
 *  The value of String str2 is 'abcd'.
    Is str1 equal to str2?: True

 3) String.Equals(String, String)
    Is str1 equal to str2?: True
*/

// Sample for String.Equals(Object)
//            String.Equals(String)
//            String.Equals(String, String)
import System.*;
import System.Text.*;

class Sample
{
    public static void main(String[] args)
    {
        StringBuilder sb = new StringBuilder("abcd");
        String str1 = "abcd";
        String str2 = null;
        Object o2 = null;

        Console.WriteLine();
        Console.WriteLine(" *  The value of String str1 is '{0}'.", str1);
        Console.WriteLine(" *  The value of StringBuilder sb is '{0}'.", 
            sb.ToString());

        Console.WriteLine();
        Console.WriteLine("1a) String.Equals(Object). Object is a " 
            + "StringBuilder, not a String.");
        Console.WriteLine("    Is str1 equal to sb?: {0}", 
            System.Convert.ToString(str1.Equals(sb)));

        Console.WriteLine();
        Console.WriteLine("1b) String.Equals(Object). Object is a String.");
        str2 = sb.ToString();
        o2 = str2;
        Console.WriteLine(" *  The value of Object o2 is '{0}'.", o2);
        Console.WriteLine("    Is str1 equal to o2?: {0}", 
            System.Convert.ToString(str1.Equals(o2)));

        Console.WriteLine();
        Console.WriteLine(" 2) String.Equals(String)");
        Console.WriteLine(" *  The value of String str2 is '{0}'.", str2);
        Console.WriteLine("    Is str1 equal to str2?: {0}", 
            System.Convert.ToString(str1.Equals(str2)));

        Console.WriteLine();
        Console.WriteLine(" 3) String.Equals(String, String)");
        Console.WriteLine("    Is str1 equal to str2?: {0}", 
            System.Convert.ToString(String.Equals(str1, str2)));
    } //main
} //Sample
/*
This example produces the following results:

 *  The value of String str1 is 'abcd'.
 *  The value of StringBuilder sb is 'abcd'.

1a) String.Equals(Object). Object is a StringBuilder, not a String.
    Is str1 equal to sb?: False

1b) String.Equals(Object). Object is a String.
 *  The value of Object o2 is 'abcd'.
    Is str1 equal to o2?: True

 2) String.Equals(String)
 *  The value of String str2 is 'abcd'.
    Is str1 equal to str2?: True

 3) String.Equals(String, String)
    Is str1 equal to str2?: True
*/

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

Community Additions

Show:
© 2015 Microsoft