Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Compare() - Method


Compare() Method


The Compare() Method is used to compare two Strings and returns :

  1. -1, if the first string is lower than the second String.

  2. 0, if both Strings are equal.

  3. 1, if the first string is greater than the second String.


Greater than and less than in case of Strings seems a little weird. But don't worry, we will explain it.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        string x = "ab";
    	string y = "qr";
    	
    	int z = string.Compare(x, y);
    	
    	if (z == -1) 
    	{ 
        	System.Console.WriteLine("First string is lower than the second String");  
        } 
        else if (z == 0)
        {
        	System.Console.WriteLine("Both Strings are equal");
        } 
        else if (z == 1)
        {
        	System.Console.WriteLine("First string is greater than the second String");
        }		 	
    }
}


Output :



  First string is lower than the second String

Now, in the above example, we have taken two Strings,


string x = "ab";

And


string y = "qr";

Now, if you see the first string ab, it looks to be a smaller letter than the second String qr(Because the letter a and b occurs much earlier than q and r).


Then we use the compare method.


int z = string.Compare(x, y);

And since the first string is smaller than the second String. The first condition of if is satisfied.


if (z == -1)
{
	System.Console.WriteLine("First string is lower than the second String");
}

And the below output is printed.


First string is lower than the second String

Similarly in the second example,


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        string x = "zy";
    	string y = "ab";
    	
    	int z = string.Compare(x, y);
    	
    	if (z == -1) 
    	{ 
        	System.Console.WriteLine("First string is lower than the second String");  
        } 
        else if (z == 0)
        {
        	System.Console.WriteLine("Both Strings are equal");
        } 
        else if (z == 1)
        {
        	System.Console.WriteLine("First string is greater than the second String");
        }		 	
    }
}


Output :



  First string is greater than the second String

Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        string x = "ab";
    	string y = "ab";
    	
    	int z = string.Compare(x, y);
    	
    	if (z == -1) 
    	{ 
        	System.Console.WriteLine("First string is lower than the second String");  
        } 
        else if (z == 0)
        {
        	System.Console.WriteLine("Both Strings are equal");
        } 
        else if (z == 1)
        {
        	System.Console.WriteLine("First string is greater than the second String");
        }		 	
    }
}


Output :



  Both Strings are equal