Comparing Strings

We can compare two strings for equality using several ways. For example, we can use the == operator to test if the two strings are equal. The == operator doesn’t compare their references, but their values even though strings are reference types.

string str1 = "Hello";
string str2 = "Hello";
string str3 = "Goodbye";

Console.WriteLine("str1 == str2 : {0}", str1 == str2);
Console.WriteLine("str1 == str3 : {0}", str1 == str3);
str1 == str2 : True
str1 == str3 : False

Alternatively, you can use the String.Compare static method to compare two strings. The following code demonstrates this.

if (String.Compare(str1, str2) == 0)
{
   Console.WriteLine("str1 is equal to str2");
}

The String.Compare method accepts two strings and returns 0 if the strings are the same, returns 1 if the value of the first string is greater than the second, and -1 if the value of the first string is less than the second string. So to determine if the strings are equal using the Compare method, you must test if the value returned is equal to 0 as seen in the code above.

How do you determine if a string is greater or less than the other? Each character of the strings is converted into their Unicode equivalent. The first character of the first string is compared to the first character of the second string. If they are equal, it will compare the second characters and so on.

Another overloaded version of String.Compare accepts a third boolean argument which determines if the comparison will ignore case sensitivity. By default, String.Compare respects casing and treats two strings with different casings as different strings. The following shows how you can compare strings while ignoring case sensitivity.

string str1 = "Microsoft";
string str2 = "microsoft";

if (String.Compare(str1, str2, true))
{
 Console.WriteLine("The strings are equal.");
}
The strings are equal.

There are more overloads for the String.Compare method as listed below.

Method Description
Compare(String, String) Compares two specified String objects.
Compare(String, String, Boolean) Compares two specified String objects, ignoring or respecting their case.
Compare(String, String, StringComparison) Compares two specified String objects. Also specifies whether the comparison uses the current or invariant culture, honors or respects casing, and uses word or ordinal sort values.
Compare(String, String, Boolean, CultureInfo) Compares two specified String objects, ignoring or respecting their case and using a culture-specific information for the comparison.
Compare(String, Int32, String, Int32, Int32) Compares substrings of two specified String objects.
Compare(String, Int32, String, Int32, Int32, Boolean) Compares substrings of two specified String objects, ignoring or respecting their case.
Compare(String, Int32, String, Int32, Int32, StringComparison) Compares substrings of two specified String objects.
Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo) Compares substrings of two specified String objects, ignoring or respecting their case, and using a culture-specific information for the comparison.

Figure 1 – String.Compare Method Overloads

Alternatively, you can use the CompareTo instance method. The following shows you how to use this method.

string str1 = "Hello";
string str2 = "Hello";

if (str1.CompareTo(str2) == 0)
{
   Console.WriteLine("The strings are equal.");
}
The strings are equal.

The CompareTo method returns an integer. Like the Compare method, If the strings are equal, it will return 0, if the string being compared is greater than the string, it will return 1, and if it is less than the comparison string, it will return -1.