Partager via


String.Equals Method (String, String, StringComparison)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function Equals ( _
    a As String, _
    b As String, _
    comparisonType As StringComparison _
) As Boolean
[SecuritySafeCriticalAttribute]
public static bool Equals(
    string a,
    string b,
    StringComparison comparisonType
)

Parameters

  • a
    Type: System.String
    The first string to compare, or nulla null reference (Nothing in Visual Basic).
  • b
    Type: System.String
    The second string to compare, or nulla null reference (Nothing in Visual Basic).
  • comparisonType
    Type: System.StringComparison
    One of the enumeration values that specifies the rules for the comparison.

Return Value

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

Exceptions

Exception Condition
ArgumentException

comparisonType is not a StringComparison value.

Remarks

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.

Examples

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)
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
'
// Sample for String.Equals(Object)
//            String.Equals(String)
//            String.Equals(String, String)
using System;
using System.Text;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      StringBuilder sb = new StringBuilder("abcd");
      String str1 = "abcd";
      String str2 = null;
      Object o2 = null;

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

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

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

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

      outputBlock.Text += "\n";
      outputBlock.Text += String.Format(" 3) String.Equals(String, String)") + "\n";
      outputBlock.Text += String.Format("    Is str1 equal to str2?: {0}", String.Equals(str1, str2)) + "\n";
   }
}
/*
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

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

Platforms

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