Hit enter after type your search item

How to compare strings 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 of 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:

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

The general syntax:

int string.Compare(string1, string2)

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


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 first two words are matched and the third is different.

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


The output:

1

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


The result:

Strings are different!

Is 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:


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:


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):

  • Zero means that the string1 and 2 are the same in the sort order.
  • If the returned value is > 0 then String 2 follows String 1.
  • 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 the three possibilities as shown below:


c-sharp String Compare

 

This div height required for enabling the sticky sidebar