Coders Packet

Compare Multiple Strings in java

By SAI PRAVEEN ALLLAMPALLY

In this packet,we are going to build a code for the compare multiple strings using Java.In this,we can perform by using compareTo()

compareTo():

"This method is used to determine whether the string is less than, equal to, or greater than the invoking string."A string is less than another string if it comes before the other in alphabetical order. And a string is greater than another string if it comes after the other in alphabetical order. 1.Less than zero It means invoking string is less than str.
2.Greater than zero It means invoking string is greater than str. 3.zero It means both the strings are equal                                                                                             

The following code gives the complete description.

public class CompareStrings
{
    public static void main(String args[])
    {
        String s1 = new String("Hello");
        String s2 = new String("Hi");
        String s3 = new String("How Are you");
        String s4 = new String("how are you");
        System.out.println("string s1 =" +s1);
        if(s1.equals("Hello"))
        System.out.println("Both are equal");
        else
        System.out.println("They are not equal");
        if(s1.equalsIgnoreCase(s2))
        System.out.println("Both are equal");
        else
        System.out.println("They are not equal");
        if(s3.equalsIgnoreCase(s4))
        System.out.println("s3 equals s4");
        else
        System.out.println("s3 not equal to s4");
        System.out.println("The returned value is:" +s1.compareTo(s2));
        System.out.println("The value is :" +s2.compareTo(s1));
    }
}

Output:

 

 

                                                                        

Download project

Reviews Report

Submitted by SAI PRAVEEN ALLLAMPALLY (9676539961SAIPRAVEEN)

Download packets of source code on Coders Packet