Advertisement
Methods | Description |
---|---|
Equals(String) | It determines whether the value of invoked String object and the value of the String passed to this method are same or not. |
Equals(Object) | It determines whether the value of invoked String object and the value of the Object passed to this method are 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. |
//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));
}
}
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
Advertisement
//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));
}
}
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
//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));
}
}
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
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. |
//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);
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement