str2.compareTo(s1: String): int

class compare
{
public static void main(String[] arg)
{
String str1 = "Welcome to Smartprogrammeron.blogspot.in";
String str2 = new String("Welcome to SmartProgrammerOn.blogspot.in");
System.out.println("str1.compareTo(str2) is " + str1.compareTo(str2));

}
}



The compareTo method can also be used to compare two strings.

 For example, consider the following code:    s1.compareTo(s2)

The method returns the value 0 if s1 is equal to s2, a value less than 0 if s1 is lexicographi- cally (i.e., in terms of Unicode ordering) less than s2, and a value greater than 0 if s1 is lexi- cographically greater than s2. The actual value returned from the compareTo method depends on the offset of the first two distinct characters in s1 and s2 from left to right. For example, suppose s1 is "abc" and s2 is "abg", and s1.compareTo(s2) returns -4. The first two characters (a vs. a) from s1 and s2 are compared. Because they are equal, the second two characters (b vs. b) are com- pared. Because they are also equal, the third two characters (c vs. g) are compared. Since the character c is 4 less than g, the comparison returns -4.

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons