【发布时间】:2014-09-06 14:49:19
【问题描述】:
我希望有人能给我解释为什么下面的代码不起作用:
//Why doesnt this work
String l = myString.substring(cut, lengthLastBtn-1);
String c = myString.substring(cut, lengthLastBtn-1);
if(l==c){
Log.i(TAG, "Correct");
}
//End
//This work!
String l = "hi";
String c = "hi";
if(l==c){
Log.i(TAG, "Correct");
}
// End
// Or if i want the Vars as in the first code i have to use the if statement like this
if(l.contains(c)){
Log.i(TAG, "Correct");
}
//End
那么,当我使用了 substring 方法后,为什么不能比较一个字符串。我什至在日志中看到它们是相同的,或者至少具有相同的文本。
【问题讨论】:
-
==按对象标识而不是内容比较字符串。 -
你应该改用
equals()方法。 -
strings that use the substring method是什么?! -
感谢您的快速回复,equals 成功了。该链接非常有用。