TechieClues TechieClues
Updated date Dec 06, 2021
In this blog, we will see how to compare two strings in C# using equals(), String.Equals(), String.Compare() and String.CompareTo() methods with examples.
  • 5.8k
  • 0
  • 0

String comparison is nothing but comparing two string values. In C# we have multiple ways to achieve this string comparison. 

Using the below ways we can check the equality of strings:

  • == operator
  • String.Equals() method
  • String.Compare() method
  • String.CompareTo() method

Using '==' Operator

When we use == operator, it compares the first and second string values. If the string values are equal then it returns true else returns false.

Example:

class StringCompareSample
{
	static void Main(string[] args)
	{
		string carName1 = "ford";
		string carName2 = "audi";
		string carName3 = "Saudi";

        // Compare two strings using == Operator
		if (carName1 == carName2)
			Console.WriteLine("Car Name 1 and Car Name 2 are the same");
		else
			Console.WriteLine("Car Name 1 and Car Name 2 are different");

        // Compare two strings using == Operator
		if (carName2 == carName3)
			Console.WriteLine("Car Name 2 and Car Name 3 are the same");
		else
			Console.WriteLine("Car Name 2 and Car Name 3 are different");
		Console.ReadKey();
	}
}  

Output:

Car Name 1 and Car Name 2 are different
Car Name 2 and Car Name 3 are the same

Using 'String.Equals()' Method

We can also use the String.Equals() to compare the two string values. "Equals()" method returns the value true when the two string values are equal otherwise returns false.

class StringCompareSample
{
	static void Main(string[] args)
	{
		string carName1 = "ford";
		string carName2 = "ford";
		
		// Compare two strings using Equals() Method - 1
		if (carName1.Equals(carName2))
			Console.WriteLine("Both Cars are the same - 1");
		else
			Console.WriteLine("Both Cars are different - 1");
   
		// Compare two strings using Sting.Equals() Method - 2
		if (String.Equals(carName1, carName2))
			Console.WriteLine("Both Cars are the same - 2");
		else
			Console.WriteLine("Both Cars are different - 2");
		Console.ReadKey();
	}
}  

Output:

Both Cars are the same - 1
Both Cars are the same - 2 

Using 'String.Compare()' method

String.Compare() method can be used to compare two string objects in C#. It returns an integer that indicates their relative position in the sort order. The return integer value can be less than zero, zero, or greater than zero. See the below table for more details,

Return value Condition
Less than 0 String 1 precedes String 2 in the sort order.
0 Both strings are in the same position in the sort order.
Geater than 0 String 1 follows String 2 in the sort order.

 

Example:

class StringCompareSample
{
	static void Main(string[] args)
	{
		string carName1 = "BMW";
		string carName2 = "BMW";                    

		// Compare two strings using String.Compare() Method
		if (String.Compare(carName1, carName1) == 0)
			Console.WriteLine("Both Cars are BMW");
		else if (String.Compare(carName1, carName1) < 0)
			Console.WriteLine($"{carName1} precedes {carName2}.");
		else if (String.Compare(carName1, carName1) > 0)
			Console.WriteLine($"{carName1} follows {carName2}.");
		Console.ReadKey();
	}
}  

Output:

Both Cars are BMW

Using 'String.CompareTo()' method

The CompareTo() method is an instance method of string class. The method compares the string value or object with the string instance and returns the integer values similar to Compare() method.

class StringCompareSample
{
	static void Main(string[] args)
	{
		string carName1 = "BMW";
		string carName2 = "BMW";                    

		// Compare two strings using String.CompareTo() Method
		if (carName1.CompareTo(carName1) == 0)
			Console.WriteLine("Both Cars are BMW");
		else if (carName1.CompareTo(carName1) < 0)
			Console.WriteLine($"{carName1} precedes {carName2}.");
		else if (carName1.CompareTo(carName1) > 0)
			Console.WriteLine($"{carName1} follows {carName2}.");
		Console.ReadKey();
	}
}  

Output:

Both Cars are BMW

 

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!