String.Equals Method (Object)
.NET Framework 3.0
Determines whether this instance of String and a specified object, which must also be a String object, have the same value.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
public boolean Equals ( Object obj )
public override function Equals ( obj : Object ) : boolean
Not applicable.
Parameters
- obj
An Object.
Return Value
true if obj is a String and its value is the same as this instance; otherwise, false.The following code example demonstrates the Equals method.
// 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 */
// 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 Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: