2

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 Answers 3

9

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Just to add for Java beginners, it is done this way because Strings are Objects.
1

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

2 Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.