java subSequence显然是真的,但只返回false值。为什么? 试图查看序列是否等于更大字符串的子序列
package testifthen;
public class TestIfThen {
public static void main(String[] args) {
String result = "01900287491234567489";
String result1 = "90028749";
if (result.subSequence(2, 10) == result1) {
System.out.println("excel");
}else {
System.out.println("not found");
}
}}
答案 0 :(得分:2)
如果没有更多信息(例如,这是什么语言),很难说。
假设这是Java,我会说你的问题是使用==
字符串而不是.equals
函数。
==
仅在它们引用同一对象时才检查字符串的内容。应该使用.equals
,因为它实际检查两个字符串中的字符是否匹配
答案 1 :(得分:1)
在Java中,在检查语义相等性时,.equals
方法应优先于==
运算符。当你检查两个值是否“意味着”同一个东西时,应该使用.equals
,而==
检查它们是否是同一个确切的对象。
答案 2 :(得分:1)
尝试使用
if (result.subSequence(2, 10).equals(result1)) {
System.out.println("excel");
} else {
System.out.println("not found");
}
由于引用不同,==
符号可能会导致它返回false。
这篇文章应该详细解释==
和equals()
之间的差异:What is the difference between == vs equals() in Java?