Advertisement



< Prev
Next >



C# String Equals() method




The Equals() method of the String class determines if the two String objects have the same value.




Some versions of the Equals() method


Methods Description
Equals(String)
It determines whether the value of invoked String object and the value of the String passed to this method is the same or not.

Equals(Object)
It determines whether the value of invoked String object and the value of the Object passed to this method is the same or not.

Equals(String, String)
It determines whether the two String objects have the same value or not.

This is a static method.

Equals(String, String, StringComparison)
It determines whether the two String objects have the same value or not. The comparison is performed on the basis of a specified field of the type StringComparison.

This is a static method.






Note :


This method performs case-sensitive equality check.




Example of Equals(String) method


Here in the next example, we have are explaining the first overloading version of the Equals() method i.e. Equals(String).

//C# Example of Equals(String) method


using System;

class StringEquals
{
public static void Main(String[] str)
{
	//Creating the first String object
	String str1 = "A sunny day";

	//Creating the second String object
	String str2 = "A sunny day";

	//Printing the first and second String objects
	Console.WriteLine("First  String is : " + str1);
	Console.WriteLine("Second String is : " + str2);

	Console.WriteLine("Are values in first and second String object same?  " + str1.Equals(str2));

	//Creating the third String object
	String str3 = "Hello321";

	//Creating the fourth String object
	String str4 = "Hello32";

	//Printing the third and fourth String objects
	Console.WriteLine("Third  String is : " + str3);
	Console.WriteLine("Fourth String is : " + str4);

	Console.WriteLine("Are values in third and fourth String object same?  " + str3.Equals(str4));
}
}


Output is :


First  String is : A sunny day
Second String is : A sunny day
Are values in first and second String object same?  True
Third  String is : Hello321
Fourth String is : Hello32
Are values in third and fourth String object same?  False


Program Analysis





Advertisement




Example of Equals(Object) method


Here in the next example, we have are explaining the second overloaded version of the Equals() method i.e. Equals(Object), which determines whether the value of invoked String object and the value of the Object passed to this method is the same or not.

//C# Example of Equals(Object) method


using System;

class A
{
public static void Main(String[] str)
{
	//Creating a String object
	String str1 = "A sunny day";

	//Creating an Object and initializing it to a value of type String
	Object ob1 = "A sunny day";

	//Printing the first and second String objects
	Console.WriteLine("String is : " + str1);
	Console.WriteLine("Object is : " + ob1);

	Console.WriteLine("Are values in the String and the Object same?  " + str1.Equals(ob1));

	//Creating a String object
	String str2 = "Hello321";

	//Creating an Object and initializing it to an object of type class A
	Object ob2 = new A();

	//Printing the String and the Object
	Console.WriteLine("String is : " + str2);
	Console.WriteLine("Object is : " + ob2);

	Console.WriteLine("Are values in the String and the Object same?  " + str2.Equals(ob2));
}
}


Output is :


String is : A sunny day
Object is : A sunny day
Are values in the String and the Object same?  True
String is : Hello321
Object is : A
Are values in the String and the Object same?  False





Example of Equals(String, String) method


Here in the next example, we have are explaining the third overloaded static version of the Equals() method i.e. Equals(String, String), which determines whether the value of two passed String objects are same or not.

//C# Example of Equals(String, String) method


using System;

class StringEquals
{
public static void Main(String[] str)
{
	//Creating the first String object
	String str1 = "A sunny day";

	//Creating the second String object
	String str2 = "A sunny day";

	//Printing the first and second String objects
	Console.WriteLine("First  String is : " + str1);
	Console.WriteLine("Second String is : " + str2);
	
	//Calling the static Equals(String, String) method
	Console.WriteLine("Are values in first and second String object same?  " + Equals(str1, str2));

	//Creating the third String object
	String str3 = "Hello321";

	//Creating the fourth String object
	String str4 = "Hello32";

	//Printing the third and fourth String objects
	Console.WriteLine("Third  String is : " + str3);
	Console.WriteLine("Fourth String is : " + str4);

	//Calling the static Equals(String, String) method
	Console.WriteLine("Are values in third and fourth String object same?  " + Equals(str3, str4));
}
}


Output is :


First  String is : A sunny day
Second String is : A sunny day
Are values in first and second String object same?  True
Third  String is : Hello321
Fourth String is : Hello32
Are values in third and fourth String object same?  False





Example of Equals(String, String, StringComparison) method


Here in the next example, we have are explaining another overloaded static version of the Equals() method i.e. Equals(String, String, StringComparison), which determines whether the value of two passed String objects are same or not. The comparison is performed on the basis of a specified field of the type StringComparison.



The StringComparison is an Enum which specifies culture, sort, case rules to be used by whem the comparison is made between String objects. Its all important 6 fields are explained below.


Fields of StringComparison Value Description
CurrentCulture 0
Compares String objects using culture-sensitive sort rules and current culture.

CurrentCultureIgnoreCase 1
Compares String objects using culture-sensitive sort rules and current culture, while ignoring the case of the String objects being compared.

InvariantCulture 2
Compares String objects using culture-sensitive sort rules and invariant culture.

InvariantCultureIgnoreCase 3
Compares String objects using culture-sensitive sort rules and invariant culture, while ignoring the case of the String objects being compared.

Ordinal 4
Compares String objects using ordinal(binary) sort rules.

OrdinalIgnoreCase 5
Compares String objects using ordinal(binary) sort rules, while ignoring the case of the String objects being compared.



Note: The Equals(String, String StringComparison) method also performs a case-sensitive search, unless you pass the a specific field of StringComparison with IgnoreCase specified in it.

//C# Example of Equals(String, String StringComparison) method of String.

using System;
using System.Globalization;

class StringEquals
{
public static void Main()
{
	//Creating the first String object
	String str1 = "A sunny day";

	//Creating the second String object
	String str2 = "A sunny day";

	//Creating the third String object
	String str3 = "A SUNNY DAY";

	//Printing the first and second String objects
	Console.WriteLine("First  String is : " + str1);
	Console.WriteLine("Second String is : " + str2);

	//String comparison made using StringComparison.CurrentCulture
	Console.WriteLine("String comparison by StringComparison.CurrentCulture:");
	
	bool boo1 = String.Equals(str1, str2, StringComparison.CurrentCulture);
	Console.WriteLine("Are" + str1 + " and " + str2 + " same? "+ boo1);


	//String comparison made using StringComparison.CurrentCultureIgnoreCase
	Console.WriteLine("String comparison by StringComparison.CurrentCultureIgnoreCase:");

	boo1 = String.Equals(str1, str3, StringComparison.CurrentCultureIgnoreCase);
	Console.WriteLine("Are" + str1 + " and " + str3 + " same? "+ boo1);


	//String comparison made using StringComparison.InvariantCulture
	Console.WriteLine("String comparison by StringComparison.InvariantCulture: ");

	boo1 = String.Equals(str1, str2, StringComparison.InvariantCulture);
	Console.WriteLine("Are" + str1 + " and " + str2 + " same? "+ boo1);



	//String comparison made using StringComparison.InvariantCultureIgnoreCase
	Console.WriteLine("String comparison by StringComparison.InvariantCultureIgnoreCase: ");

	boo1 = String.Equals(str1, str3, StringComparison.InvariantCultureIgnoreCase);
	Console.WriteLine("Are" + str1 + " and " + str3 + " same? "+ boo1);


	//String comparison made using StringComparison.Ordinal
	Console.WriteLine("String comparison by StringComparison.Ordinal: ");

	boo1 = String.Equals(str1, str2, StringComparison.Ordinal);
	Console.WriteLine("Are" + str1 + " and " + str2 + " same? "+ boo1); 


	//String comparison made using StringComparison.OrdinalIgnoreCase
	Console.WriteLine("String comparison by StringComparison.OrdinalIgnoreCase: ");

	boo1 = String.Equals(str1, str3, StringComparison.OrdinalIgnoreCase);
	Console.WriteLine("Are" + str1 + " and " + str3 + " same? "+ boo1);
}
}


Output is :


First  String is : A sunny day
Second String is : A sunny day
String comparison by StringComparison.CurrentCulture:
AreA sunny day and A sunny day same? True
String comparison by StringComparison.CurrentCultureIgnoreCase:
AreA sunny day and A SUNNY DAY same? True
String comparison by StringComparison.InvariantCulture:
AreA sunny day and A sunny day same? True
String comparison by StringComparison.InvariantCultureIgnoreCase:
AreA sunny day and A SUNNY DAY same? True
String comparison by StringComparison.Ordinal:
AreA sunny day and A sunny day same? True
String comparison by StringComparison.OrdinalIgnoreCase:
AreA sunny day and A SUNNY DAY same? True





Please share this article -




< Prev
Next >
< C# LastIndexOf() Method
C# Split() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement