Comparision of two strings?

Comparision of two strings?

Previous Home Next

 

This example shows how the two strings are compare using java string object.


 Java string class use three methods to compare strings.

intCompareTo(String anotherString)
int compareTo( Object obj )

and int compareToIgnoreCase( String anotherString )


 

class CompareEaxmple
{
public static void main(String[] args)
{
String str = "Hello R4R";
String anotherString = "hello r4r";
Object objStr = str;
System.out.println( str.compareTo(anotherString) );//compare two strings case sensitive
System.out.println( str.compareToIgnoreCase(anotherString) );// compare two strings ignore character case
System.out.println( str.compareTo(objStr) );// compare string with object
}
}


Previous Home Next