Comparing Strings in Java

Java provides a variety of methods for comparing String objects. It signifies one String to be greater than" or  less than" another String, consider the method of alphabetizing a series of last names
Example :- We place the word "John" before "Smith" because the first letter of "John" comes before the first letter of "Smith" in the alphabet. But the alphabet is more than just a list of 26 letters- it is an ordered list of characters. Each letter occurs in  specific positions within with the list."Z" is more than just a letter of the alphabet "Z"  is specifically the twenty sixth letter of the alphabet.

 When the computer compares two Strings, it actually compares the numeric codes of the character in the string.

There are three ways to compare string in Java:

1. By equals () method
2. By = = operator
3. By compareTo () method


1. String compare by equals() method :-  It is used to compare a String  for equality in Java.

public class Stringcompare{
    public static void main ( String args[]) 
    {
        String s1, s2, s3, s4, output;
        s1 = new String ("dev");
        s2 = new String ("studio");
        s3 = new String ("online");
        s4 = new String ("devstudioonline");
        output = "s1 =" + s1 +"\ns2 = "+s2 + "\ns3 =" + s3 + "\ns4= " +s4 +"\n\n";
       
        // test for equality
        if ( s1.equals("hello") )
        output += "s1 equals hello\n";
        else
        output += "s1 does not equal hello\n";
 System.out.println(output);
          
        
    }
    
}

Output
s1 =dev
s2 = studio
s3 =online
s4= devstudioonline

s1 does not equal hello

String class supports the two methods in Java.

First is public boolean equals it is a another object, it is helpful to compare the string of a particular object.
Second is public boolean equalsIgnoreCase it is used to compare the String to different string and ignoring case.

public class Stringcompare{
    public static void main ( String args[]) 
    {
        String s1, s2, s3, s4, output;
        s1 = new String ("dev");
        s2 = new String ("studio");
        s3 = new String ("online");
        s4 = new String ("devstudioonline");
        output = "s1 =" + s1 +"\ns2 = "+s2 + "\ns3 =" + s3 + "\ns4= " +s4 +"\n\n";

// test for equality-- ignore case
        
         if (s3.equalsIgnoreCase ( s4 ))
         output += "s3 equal s4\n";
         else 
        output += "s3 does not equal s4\n";
System.out.println(output);
          
        
    }
    
}

Output
s1 =dev
s2 = studio
s3 =online
s4= devstudioonline

s3 does not equal s4

2. String compare by == operator :- It is used to compare a string by using a == equality operator .Operator == has different functionality when it is used to compare references and when it is used to compare values of primitive data types.

public class Stringcompare{
    public static void main ( String args[]) 
    {
        String s1, s2, s3, s4, output;
        s1 = new String ("dev");
        s2 = new String ("studio");
        s3 = new String ("online");
        s4 = new String ("devstudioonline");
        output = "s1 =" + s1 +"\ns2 = "+s2 + "\ns3 =" + s3 + "\ns4= " +s4 +"\n\n";

// test for equality with ==
         if (s1 == "hello")
         output += "s1 equals hello/n";
         else
         output += "s1 does not equal \"hello\"\n";
 System.out.println(output);
          
        
    }
    
}

Output
s1 =dev
s2 = studio
s3 =online
s4= devstudioonline

s1 does not equal "hello"

3. String compare by compareTo() method :-  The compareTo returns 0 if the String are equal, a negative number if theString that request compareTo is less than the String that is passed as an argument and a positive number if the Stringthat request compareTo is greater than the String that is passed as an argument. Method compareTo uses a lexicographical comparison.

public class Stringcompare{
    public static void main ( String args[]) 
    {
        String s1, s2, s3, s4, output;
        s1 = new String ("dev");
        s2 = new String ("studio");
        s3 = new String ("online");
        s4 = new String ("devstudioonline");
        output = "s1 =" + s1 +"\ns2 = "+s2 + "\ns3 =" + s3 + "\ns4= " +s4 +"\n\n";
 output +=
        "\ns1.compareTo ( S2 ) is " + s1.compareTo ( s2 ) +
         "\ns2.compareTo ( S1 ) is " + s1.compareTo ( s1 ) +
          "\ns3.compareTo ( S3 ) is " + s1.compareTo ( s3 ) +
          "\ns4.compareTo ( S4 ) is " + s1.compareTo ( s4 ) +
          "\n\n";
 System.out.println(output);
          
        
    }
    
}

 Output
s1 =dev
s2 = studio
s3 =online
s4= devstudioonline
s1.compareTo ( S2 ) is -15
s2.compareTo ( S1 ) is 0
s3.compareTo ( S3 ) is -11
s4.compareTo ( S4 ) is -12

Other Example

public class Stringcompare{
    public static void main ( String args[]) 
    {
        String s1, s2, s3, s4, output;
        s1 = new String ("dev");
        s2 = new String ("studio");
        s3 = new String ("online");
        s4 = new String ("devstudioonline");
        output = "s1 =" + s1 +"\ns2 = "+s2 + "\ns3 =" + s3 + "\ns4= " +s4 +"\n\n";
// test regionMatches (case sensitive)
          if (s3.regionMatches (0, s4 ,0, 5))
          output +="First 5 characters of s3 and s4 match\n";
          else 
          output +=
          "First 5 characters of s3  and s4  do not match\n";
          
          // test regionMatches (case ignore)
          if (s3.regionMatches (true,0, s4 ,0, 5))
          output +="First 5 characters of s3 and s4 match\n";
          else 
          output +=
          "First 5 characters of s3  and s4  do not match\n";
          System.out.println(output);
          
        
    }
    
}

Output
s1 =dev
s2 = studio
s3 =online
s4= devstudioonline

First 5 characters of s3 and s4 do not match
First 5 characters of s3 and s4 do not match

 




Keywords: