Java String compareTo Example
String class compareTo method example
String class compareTo method example. This example shows you how to use compareTo method.This method Compares two strings lexicographically.
Syntax:-compareTo(String anotherString)
Here is the Code:-
/**
* @(#) CompareTo.java
*Program that Compares two strings lexicographically, ignoring case considerations.
* @Version 01-May-2008
* @author Rose India Team
*/
public class CompareTo
{
public static void main(String[] args)
{
String str1 = "Welcome to RoseIndia team";
String str2 = "Welcome to RoseIndia ";
// Compare the two strings.
int result = str1.compareTo(str2);
if (result < 0)
{
// displays str1 is less than str 2
System.out.println(str1 + "is less than" + str2);
}
else if (result == 0)
{
// displays str1 is equal to str 2
System.out.println(str1 +"is equal to" + str2);
}
else
{
// displays str1 is greater than str 2
System.out.println(str1 +"is greater than" + str2);
}
}
} |
|