How to compare Strings in Java

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

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

0

0

你可能感兴趣的:(How to compare Strings in Java)