How to Compare Strings in C#?

Featured image reflecting string comparison in C#

Depending on the requirement, C# offers a few ways to compare strings.For example, if you only require to check whether two strings are the same (ignoring case). Similarly, you may compare the strings based on linguistic and ordinal.

In this tutorial on C# string comparison, I will show you examples of the following methods/classes:

  • String Compare method
  • Equals
  • == operator
  • CompareTo

The String Compare method

The string Compare method can be used to see if two strings are the same or which string is greater in terms of ASCII value.

The Compare method returns three possible values as int:

Return value Description
0 If the return value is 0 then both strings are the same.
> 0 If the return value is 1 or greater than 0 then the first string is greater than the second.
< 0 Similarly, if the Compare method returns -1 or less than 0 then the second string is greater.

 

The general syntax:

int string.Compare(string1, string2)

See the example below to learn how it works. To demonstrate that, I used simple letters ‘a’, ‘b’, ‘c’ as the values for strings and compared:

using System;
class string_comparison_demo

{
    static void Main()
    {
        string str_1 = "a";
        string str_2 = "b";
        string str_3 = "c";
        string str_4 = "a";

        Console.WriteLine(string.Compare(str_1, str_2));
        Console.WriteLine(string.Compare(str_3, str_2));
        Console.WriteLine(string.Compare(str_1, str_4));

        Console.ReadLine();
    }
}

The result of three comparisons:

-1

1

0

Another example with a sentence to see how Compare works

The following example uses two strings, out of which the first two words are matched and the third is different.

To understand how the Compare method works; whether it only checks the first word or later – this should make things clearer:

using System;

class string_comparison_demo

{
    static void Main()
    {
        string string_1 = "C# is cool";
        string string_2 = "C# is awesome";
        Console.WriteLine(string.Compare(string_1, string_2));

        Console.ReadLine();
    }
}

The output:

1

From the output, it is evident that as Compare method found “c” in the first string as compared to “a” in the second; although the “awesome” is a bigger word than “cool” – it returned 1. That means the first sentence is greater than the second.

Using the Equals method for string comparison

The Equals method of the String is used to determine if two string objects are the same or not.

The Equals return a Boolean; True if strings are the same and False otherwise.

The general syntax for using the Equals method:

public static bool Equals (string str1, string str2);

Following is an example of using the Equals method. For that, we have two string objects and see how these are compared:

using System;

class string_comparison_demo

{

    static void Main()
    {
        string str_equ1 = "C# compare";
        string str_equ2 = "C Sharp compare";

        if (String.Equals(str_equ1, str_equ2))
        {
            Console.WriteLine("Both Strings are same!");
        }
        else
        {
            Console.WriteLine("Strings are different!");
        }

        Console.ReadLine();
    }
}

The result:

Strings are different!

Is the Equals method case-sensitive?

Yes, the Equals method is case-sensitive. In the following simple example, we used “C#” and “c#” and see the result:

using System;

class string_comparison_equals

{
    static void Main()
    {
        string str_equ1 = "C# cool";
        string str_equ2 = "c# cool";

        if (String.Equals(str_equ1, str_equ2))

        {
            Console.WriteLine("Same!");
        }
        else
        {
            Console.WriteLine("Different!");
        }

        Console.ReadLine();
    }
}

The output:

Different!

Using == operator for string comparison

For simple string comparison, you may also use the ‘==’ operator. If strings are same, it returns True otherwise false.

See the example below where we checked equality of two strings by == operator:

using System;

class string_comparison_example
{
    static void Main()
    {
        string str_1 = "C# by == operator";
        string str_2 = "C# equals";

        if (str_1 == str_2)
        {
            Console.WriteLine("Same Strings!");
        }
        else
        {
            Console.WriteLine("Different Strings!");
        }
        Console.ReadLine();
    }
}

The result:

Different Strings!

The CompareTo method for string comparison

As per official documentation, the CompareTo method checks this instance of the object with the specified string object.

The CompareTo indicates whether the object instance precedes, follows, or appears in the same position in the sort order as compared to the specified object (string in this case).

The return value by CompareTo is an int type with these possibilities (suppose String1 is compared to String2):

Return value Description
0 0 means that the string1 and 2 are the same in the sort order.
> 0 If the returned value is > 0 then String 2 follows String 1.
< 0 For value < 0, the String1 precedes String2.

In the following example, we have four string variables that are compared by using the CompareTo method. The results show all three possibilities as shown below:

using System;

class string_comparison_example

{
    static void Main()
    {
        string s1 = "a";
        string s2 = "b";
        string s3 = "c";
        string s4 = "a";

        int res1 = s1.CompareTo(s2);
        int res2 = s3.CompareTo(s2);
        int res3 = s1.CompareTo(s4);

        Console.WriteLine("First CompareTo Result: " + res1);
        Console.WriteLine("Second CompareTo Result: " + res2);
        Console.WriteLine("Third CompareTo Result: " + res3);

        Console.ReadLine();
    }
}

c-sharp String Compare

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!