I have two "equal" Strings. When I print both Strings they look exactly the same in the screen. But when I compare the Strings the result is "false" and using .length in both Strings the result is 174 for the first String and 171 for the second. I have deleted all whitespaces and everything to set the Strings in one line.
String 1:
<docxmlns="http://example.com/default"xmlns:x="http://example.com/x"><aa1="1"a2="2">123</a><bxmlns:y="http://example.com/y"a3=""3""y:a1="1"y:a2="2">cdf</b></doc>
String 2:
<docxmlns="http://example.com/default"xmlns:x="http://example.com/x"><aa1="1"a2="2">123</a><bxmlns:y="http://example.com/y"a3=""3""y:a1="1"y:a2="2">cdf</b></doc>
String 1 length: 174
String 2 length: 171
I copied both Strings from Netbeans console, as you can see they are equals but they have different lengths.
Thanks.
-
2when you say compare, what does that mean? Can you post your compare code. It does sound like a trim or character encoding problem.joostschouten– joostschouten2011年06月16日 16:12:51 +00:00Commented Jun 16, 2011 at 16:12
-
if (archCan.equals(arch))... I use this method to compare, and I use trim before the comparison. I think it could be an encoding problem.arkhadi– arkhadi2011年06月16日 16:16:22 +00:00Commented Jun 16, 2011 at 16:16
-
what does if (archCan.trim().equals(arch.trim())) do?joostschouten– joostschouten2011年06月16日 16:18:29 +00:00Commented Jun 16, 2011 at 16:18
-
The same, they are not equal.arkhadi– arkhadi2011年06月16日 16:22:21 +00:00Commented Jun 16, 2011 at 16:22
-
Then I'm pretty sure its an encoding problem. Try highlycaffeinated's answer.joostschouten– joostschouten2011年06月16日 16:24:34 +00:00Commented Jun 16, 2011 at 16:24
5 Answers 5
When you are reading it in your java program maybe the string contains newline characters ("\n\r" in Windows) which can alter the length and equality in both strings.
1 Comment
Call getBytes() on both strings and print the results. I'm betting you have an encoding difference.
Comments
Try to print them as characters:
System.out.println( Arrays.toString( yourString.toCharArray() );
This should allow you to see where the non-printable characters (or whatever differs) are, as the output for "abc" will be "[a, b, c]".
Comments
I have deleted all whitespaces and everything to set the Strings in one line.
White space is important and may be the deciding factor in your equals problem here. As a test to see if it is, strip the white space from both strings then perform an equals check.
Comments
Did you cut & paste the strings from an external source, such as a PDF? Some years ago I had a similar issue, and I discovered that strings copied from PDFs sometimes include some invisible characters (I used Acrobat Reader).