开发者

comparing string values

I want to compare 2 strings. My first value is in 'list[0][0]' variable and the second value is in item[0]. But when I am comparing the 2 strings using 'if' statement, I don't get the answer.

if(selected_list[0][0]==items[0]) { // some code } it is not working. But when I am hard-coded these values, it is working fine. if("banana"=="banana") { // some code } Please give me开发者_运维百科 the solution? Thank you..


Here is an explanation of how strings should be compared and the different options for doing so. They aren't as simple as comparing int's.

if (string1.equals(string2))


You have to compare it as [list[0][0] isEqualToString:items[0]] otherwise you are comparing their addresses not the values.


Use the compareTo() or equals() method of one of your strings, passing the other string as argument.

string1.equals(string2)
// returns true if both strings are equal

string1.compareTo(string2)
// returns 0 if both strings are equal
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜