Export (0) Print
Expand All

String.Equals Method (String, String)

Determines whether two specified String objects have the same value.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function Equals ( _
	a As String, _
	b As String _
) As Boolean

Parameters

a
Type: System.String
The first string to compare, or Nothing.
b
Type: System.String
The second string to compare, or Nothing.

Return Value

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

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

The following code example demonstrates the Equals method.


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

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim sb As New StringBuilder("abcd")
      Dim str1 As [String] = "abcd"
      Dim str2 As [String] = Nothing
      Dim o2 As [Object] = Nothing

      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format(" *  The value of String str1 is '{0}'.", str1) & vbCrLf
      outputBlock.Text += String.Format(" *  The value of StringBuilder sb is '{0}'.", sb.ToString()) & vbCrLf

      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format("1a) String.Equals(Object). Object is a StringBuilder, not a String.") & vbCrLf
      outputBlock.Text += String.Format("    Is str1 equal to sb?: {0}", str1.Equals(sb)) & vbCrLf

      outputBlock.Text &= vbCrLf
      outputBlock.Text &= "1b) String.Equals(Object). Object is a String." & vbCrLf
      str2 = sb.ToString()
      o2 = str2
      outputBlock.Text += String.Format(" *  The value of Object o2 is '{0}'.", o2) & vbCrLf
      outputBlock.Text += String.Format("    Is str1 equal to o2?: {0}", str1.Equals(o2)) & vbCrLf

      outputBlock.Text &= vbCrLf
      outputBlock.Text &= " 2) String.Equals(String)" & vbCrLf
      outputBlock.Text += String.Format(" *  The value of String str2 is '{0}'.", str2) & vbCrLf
      outputBlock.Text += String.Format("    Is str1 equal to str2?: {0}", str1.Equals(str2)) & vbCrLf

      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format(" 3) String.Equals(String, String)") & vbCrLf
      outputBlock.Text += String.Format("    Is str1 equal to str2?: {0}", [String].Equals(str1, str2)) & vbCrLf
   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
'


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft