Friday, 2 August 2013

compare Strings in Java

The example compares two strings by using str compareTo (string) , str compareToIgnoreCase(String) and str compareTo(object string) methods of string class and returns the ascii difference of first odd characters of compared strings .

Example of comparing Strings in Java

public class StringCompare{ 
public static void main(String args[]){ 
String str = "Nice World"; 
String anotherString = "nice world"; 
Object objStr = str; 

System.out.println( str.compareTo(anotherString) ); 
System.out.println( str.compareToIgnoreCase(anotherString) ); 
System.out.println( str.compareTo(objStr.toString())); 


}

Output

-32 

1 comment:

  1. your blog is an excellent am learning java from this blogg only so pls add more abt java...poda

    ReplyDelete