.NET Framework Class Library
StringEquals Method (String, String)

Determines whether two specified String objects have the same value.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Shared Function Equals ( _
	a As [%$TOPIC/1hkt4325_en-us_VS_110_2_0_0_0_0%], _
	b As [%$TOPIC/1hkt4325_en-us_VS_110_2_0_0_0_1%] _
) As [%$TOPIC/1hkt4325_en-us_VS_110_2_0_0_0_2%]
public static [%$TOPIC/1hkt4325_en-us_VS_110_2_0_1_0_0%] Equals(
	[%$TOPIC/1hkt4325_en-us_VS_110_2_0_1_0_1%] a,
	[%$TOPIC/1hkt4325_en-us_VS_110_2_0_1_0_2%] b
)
public:
static [%$TOPIC/1hkt4325_en-us_VS_110_2_0_2_0_0%] Equals(
	[%$TOPIC/1hkt4325_en-us_VS_110_2_0_2_0_1%]^ a, 
	[%$TOPIC/1hkt4325_en-us_VS_110_2_0_2_0_2%]^ b
)
static member Equals : 
        a:[%$TOPIC/1hkt4325_en-us_VS_110_2_0_3_0_0%] * 
        b:[%$TOPIC/1hkt4325_en-us_VS_110_2_0_3_0_1%] -> [%$TOPIC/1hkt4325_en-us_VS_110_2_0_3_0_2%]
public static function Equals(
	a : [%$TOPIC/1hkt4325_en-us_VS_110_2_0_4_0_0%], 
	b : [%$TOPIC/1hkt4325_en-us_VS_110_2_0_4_0_1%]
) : [%$TOPIC/1hkt4325_en-us_VS_110_2_0_4_0_2%]

Parameters

a
Type: SystemString

The first string to compare, or .

b
Type: SystemString

The second string to compare, or .

Return Value

Type: SystemBoolean
true if the value of a is the same as the value of b; otherwise, false. If both a and b are , the method returns true.
Remarks

This method performs an ordinal (case-sensitive and culture-insensitive) comparison.

Examples

The following example demonstrates the Equals method.

' Sample for String.Equals(Object) 
'            String.Equals(String) 
'            String.Equals(String, String) 
Imports System
Imports System.Text

Class Sample
   Public Shared Sub Main()
      Dim sb As New StringBuilder("abcd")
      Dim str1 As [String] = "abcd" 
      Dim str2 As [String] = Nothing 
      Dim o2 As [Object] = Nothing

      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))
   End Sub 'Main
End Class '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 
'
// 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) 
using namespace System;
using namespace System::Text;
int main()
{
   StringBuilder^ sb = gcnew StringBuilder( "abcd" );
   String^ str1 = "abcd";
   String^ str2 = nullptr;
   Object^ o2 = nullptr;
   Console::WriteLine();
   Console::WriteLine( " *  The value of String str1 is '{0}'.", str1 );
   Console::WriteLine( " *  The value of StringBuilder sb is '{0}'.", sb );
   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
*/
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8