我有两个“相等”的字符串。当我打印两个字符串时,它们在屏幕上看起来完全相同。但是,当我比较字符串时,结果为“false”,并且在两个字符串中使用 .length 时,第一个字符串的结果为 174,第二个字符串的结果为 171。我已删除所有空格以及将字符串设置为一行的所有内容。
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
我从 Netbeans 控制台复制了两个字符串,您可以看到它们是相等的,但长度不同。
谢谢。
最佳答案
当您在 java 程序中读取它时,该字符串可能包含换行符(Windows 中的 "\n\r"
),这可能会改变两个字符串的长度和相等性。
关于java - Java 中的 equals 字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6375014/