java - Java 中的 equals 字符串问题

标签 java string

我有两个“相等”的字符串。当我打印两个字符串时,它们在屏幕上看起来完全相同。但是,当我比较字符串时,结果为“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="&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

我从 Netbeans 控制台复制了两个字符串,您可以看到它们是相等的,但长度不同。

谢谢。

最佳答案

当您在 java 程序中读取它时,该字符串可能包含换行符(Windows 中的 "\n\r"),这可能会改变两个字符串的长度和相等性。

关于java - Java 中的 equals 字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6375014/

相关文章:

java - 在 Java 8 中使用 .class 进行转换?

C++ 做它想做的事 | Char[] cout 和 inverted char[]

c - 读入一个字符串并比较它 C

java - 我编写了这段代码,用于从字符串中删除特定单词

windows - Maven 中的 Java_home

java - 假装REST客户端: FactoryBean threw exception on object creation

python - 获取给定位置周围的单词

string - 在 latex 中存储文本字符串,然后向其中添加其他文本(连接)

java.lang.IllegalArgumentException : Invalid format: "2018-08-24T��:��:��" is malformed at "��:��:��"

Java - PHP 的 URLConnection MySQL 的 $_GET 方法