tags:

views:

45

answers:

3

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..

+3  A: 

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))
colithium
thank you so much
Maya
Just to add for Java beginners, it is done this way because Strings are *Objects*.
ShadowGod
+1  A: 

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

dkk
Wouldn't that apply to iPhone development??
colithium
Oops, wrong language, but it works the same way anyway. Just the way to write it is different (list[0][0]).equals(items[0])
dkk
+1  A: 

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
DavLink
thanks for these info...
Maya