0

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="&quot;3&quot;"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="&quot;3&quot;"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.

6
  • 2
    when you say compare, what does that mean? Can you post your compare code. It does sound like a trim or character encoding problem. Commented 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. Commented Jun 16, 2011 at 16:16
  • what does if (archCan.trim().equals(arch.trim())) do? Commented Jun 16, 2011 at 16:18
  • The same, they are not equal. Commented Jun 16, 2011 at 16:22
  • Then I'm pretty sure its an encoding problem. Try highlycaffeinated's answer. Commented Jun 16, 2011 at 16:24

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. The problem was with "\r" I deleted "\n" and whitespaces but I didn't do anything with "\r".
2

Call getBytes() on both strings and print the results. I'm betting you have an encoding difference.

Comments

2

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

0

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

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.