find and solve || findandsolve.com
Please wait.....

C# String Compare Method

In the C# method String.Compare method compares two strings. We can also use C# String.Equals method and StringComparer class and its method.The C# Compare() method is used to compare first string and second or first string with second string lexicographically. It returns an integer value.If both strings are equal the  returns 0 and  If first string is greater than second string the  returns 1 else it returns -1.

We can check the equality of strings using two ways:

  • Using == operator
  • Using Equals() method


Using String.Compare

Example
    using System;    
    public class StringExample
    {
        public static void Main(string[] args)
        {
    string s1 = "Test";
            string s2 = "Test";
            string s3 = "Demo";
            string s4 = "mello";
            Console.WriteLine(string.Compare(s1,s2));
            Console.WriteLine(string.Compare(s2,s3));
            Console.WriteLine(string.Compare(s3,s4));
        }
    } 

output
 0
 1
-1


Using String.Equals

The simplest form of comparting two string for the same value is using String.Equals method. If both strings are equal, the method returns true ; else returns false.

Example
    using System;   
    public class CompareExample
    {         public static void Main(string[] args)
        {
          string compareFirst = "Sylvia Neupane";
          string compareSecond = "Devin Neupane";
          string compareThird = "Rilesi Rahol";
    string compareForth = "Sylvia Neupane";
// Compare strings using String.Equals
if (String.Equals(compareFirst, compareSecond))
    Console.WriteLine("compareFirst and compareSecond have same value.");
else     Console.WriteLine("compareFirst and compareSecond are different.");
if (String.Equals(compareFirst, compareThird))
    Console.WriteLine("compareFirst and compareThird have same value.");
else
    Console.WriteLine("compareFirst and compareThird are different.");
  if (String.Equals(compareFirst, compareForth))
    Console.WriteLine("compareFirst and compareForth have same value.");
else
    Console.WriteLine("compareFirst and compareForth are different.");
        }
    }    

output
compareFirst and compareSecond are different.
compareFirst and compareThird are different.
compareFirst and compareForth have same value.


Using CompareTo Method

CompareTo method is an instance method of string class. It compares a value (either a string or on object) with a string instance. Return values of this method are same as the Compare method.

    using System;    
    public class StringExample
    {
        public static void Main(string[] args)         {  string first = "Sylvia Neuane";
           string second = "Devin Neupane";
  if (first.CompareTo(second) == 0)
                Console.WriteLine("Both strings have same value.");
            else if (first.CompareTo(second) < 0)
                 Console.WriteLine("{author1} precedes {author2}.");
            else if (first.CompareTo(second) > 0)
               Console.WriteLine("{first} follows {second}.");
        }
    }    
output
{first} follows {second}.


Compare Case-Sensitive Strings

Both, ==  and Equals() method compares the content of strings. So, there is no difference between them when you compare strings case-sensitive and in en culture.

False Example
   using System;   
    public class StringExample
    {
        public static void Main(string[] args)
        {
    string str1 = "Nepal";
      string str2 = "nepal";
     Console.WriteLine(str1 == str2);
    Console.WriteLine(str1.Equals(str2));
        }
    }    

output
Faslse
False
True Example
    using System;    
    public class StringExample
    {
        public static void Main(string[] args)         {
    string first= "Nepal";
      string second= "Nepal";
     Console.WriteLine(first== second);
    Console.WriteLine(first.Equals(second));
        }
    }  

output

True
True


Compare null String

    using System;    
    public class StringExample
    {
        public static void Main(string[] args)
        {
      string first = "Nepal";
            string second = null;
            first == str2; // false
            second.Equals(str2); // false
            first.Equals(str1); // NullReferenceException
        }
    }    

in the given above example , == operator there is no problem with  if a string is null. But, calling the Equals() method on null will throw the NullReferenceException. So, you have to make sure a string is not null before calling the Equals() method.