Author | Topic: s.compareTo() |
Indy greenhorn |
posted April 30, 2000 06:31 PM
I am having some language difficulty with String's method, compareTo(). What does "a string is lexicographically less than the string argument" mean? Indy
|
maha anna bartender |
posted April 30, 2000 09:56 PM
Indy, Take a deep breath and read on.. regds maha anna Char chart (lexographically ordered) ...1 2 3 4 5 6 7 8 9 10 .....A B C D ...a b c d...... - char 'A' lexographically precedes char 'a' - char '1' lexographically precedes char '2' - In otherwords any char (c1) whose UNICODE VALUE is lesser than Unicode value chart 1=49 5=53 A=65 a=97 Having said that, what str1.compareTo(String str2) does is, this method just takes these 2 strings. Let us assume String s1 is in our left hand and String s2 is in our right hand. What you do is , just compare each char from index 0 to For example Compare While doing so, if you encounter a char from str1 whose value is NOT SAME as that of , the corresponding char of str2, this means, In our example, the check breaks out in the first char itself. So what s1.compareTo(s2) does is it just finds out the very first differing char from str1 and returns the differnece. In our case it is 97-65 = 32. Also note that this value may be -ve also. When will you get -ve value? When the compared char from str1 is less than the corr. compared char of str2 For example the reverse of the above example If all goes well, which means BOTH strings are SAME in contents, (i.e) s1.equals(s2) is true, then the checks are all through from starting to end and there is NO DIFFERENCE in unicode value so 0(zero) is returned. Also note that if the first string str1's length is LESS than str2's length , then there is no point in checking char by so s1.compareTo(s2) = 2 - 4 = -2; Simillarly str1 = "ABCD" so s1.compareTo(s2) = 4 - 2 = 2; ************ THE END ************** [This message has been edited by maha anna (edited April 30, 2000).]
|
maha anna bartender |
posted April 30, 2000 09:58 PM
And here is the sample program for you to test the above said inforamtion. regds
|
Indy greenhorn |
posted May 01, 2000 08:14 AM
That's an excellent explaination! maha. Cleared everything out. I don't know how to say thank you. Did you say you were gonna take the exam?? I won't doubt it if I am told you ever participated writing a java book. Indy
|
maha anna bartender |
posted May 01, 2000 07:33 PM
Are you always generous in prising. regds maha anna
|
maha anna bartender |
posted May 01, 2000 07:35 PM
I forgot to post the output of the program. Here it is. Output ------ ABCD.compareTo(abcd) = -32 // ('A' - 'a') = (65-97) = -32 abcd.compareTo(ABCD) = 32 // ('a' - 'A') = (97-65) = 32 1234.compareTo(5678) = -4 // ('1' - '5') = (49-53) = -4 5678.compareTo(1234) = 4 // ('5' - '1') = (53-49) = 4 AB.compareTo(ABCD) = -2 // ["AB".length() - "ABCD".length() ] ABCD.compareTo(AB) = 2 // ["ABCD".length() - "AB".length() ] ab.compareTo(abcd) = -2 // same as above abcd.compareTo(ab) = 2 // same as above regds
|
| | |