0
1 answers
0
compareTo() function in java is used to compare two strings lexicographically. It returns the difference of the ASCII codes of the first dissimilar character.
For example, if you have two strings,
String a = "EAST", b = "BEST;
and you wish to find out the string which comes alphabetically before the other then you compare them with compareTo() function as shown in the following code snippet:
if(a.compareTo(b) < 0 )
System.out.println(a+ " comes before " + b + " in English dictionary");
else
System.out.println(b+ " comes before " + a + " in English dictionary");
khanmansoor
Feb 10, '13
Log in to your account to answer this questions.