This topic has not yet been rated - Rate this topic

String.Equals Method (Object)

Determines whether this instance and a specified object, which must also be a String object, have the same value.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Overrides Function Equals ( _
	obj As Object _
) As Boolean

Parameters

obj
Type: System.Object

The string to compare to this instance.

Return Value

Type: System.Boolean
true if obj is a String and its value is the same as this instance; otherwise, false.

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

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 
'

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.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

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.

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.