Monday, February 24, 2014

Comparing Strings

package javaFAQsolutions;

public class comparingstrings {
   
    public static void main(String arg[]){
       
        //How to compare two strings in Java?
       
        //Ans: •The best would be using s1.equalsIgnoreCase(s2): (see javadoc)
        //•You can also convert them both to upper/lower case and use s1.equals(s2)
       String s1= "Are you there";
       String s2="are you there";
       if(s1.equalsIgnoreCase(s2)) { //method for comparing strings
//if(s1.equals(s2)) ---will compare string with same case
           System.out.println("both strings are equal");
       }
       else
       {
           System.out.println("both strings are not equal");
       }
          
    }

}

No comments:

Post a Comment