Actually i'm comparing two string which returns true when i use equals method. Whereas when i use compareTo method it returns 22.
Also i want to know at what place those two Strings differ. Using java how do i find this?
Thanks in advance.
Actually i'm comparing two string which returns true when i use equals method. Whereas when i use compareTo method it returns 22.
Also i want to know at what place those two Strings differ. Using java how do i find this?
Thanks in advance.
It's a very strange result, since the contract for compareTo(String)
says that two equal objects defined by equal
method should be equal defined by compareTo
method as well:
from javadoc String#compareTo(String)
:
"Returns: the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument."
Would you mind posting these two strings?
Usually the compareTo Methods shows you, if the string given as argument is lexicographically equal (returns 0), greater (returns something <0) or smaller (returns something > 0).
According to the documentation of compareTo()
[...] If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:
this.charAt(k)-anotherString.charAt(k)
If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
this.length()-anotherString.length()
Hence, 22 is not the position of the differing character in the two strings, but the distance between the first dissimilar characters.
As for your second question, I believe a simple iteration through the characters in both strings will allow you to pinpoint easily the first character position where they differ
According to String compareTo(String anotherString),
Returns: the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.
So, if both Strings are equal, comparing them must return 0.
If compareTo returns 22 it means the string are not equal. The equals method should show false. Could you show us your code?
Something is seriously wrong here. Most likely, you are comparing different pairs of Strings when you are comparing with with equals
and with compareTo
.
The chances of Sun's implementation of String getting this wrong are zero (IMO). And the same applies for any vendor implementation derived from the Sun codebase. It is just possible that some non-Sun codebase may have a bug in the String implementation, but this is the sort of thing that would have shown up in other peoples' code years ago.
For the record, the contract for equals
and compareTo
are that equals
returns true
when compareTo
returns 0
, and vice versa. Your observation, if correct, violates this. But that's a big "if".