I would like to know how to compare a string with an array, i.e., if my array list has {"abc", "pqr", "xyz"} and the new string lets say "mno" is typed, it should compare with my previous array list. How can I do this? Thanks in advance.
4 Answers
Look at the NSArray documentation...
BOOL hasString = [your_array containsObject:your_string];
4 Comments
sujay
can we use it in if condition
Dov
@sameer, yes you can:
if ([your_array containsObject:your_string]) {}dcrow
This is only a good solution if you are not interested in the actual string contents - which is usually not the case. The containsObject call uses isEquals to compare each item.
Mihai Costiug
@crow, you're the only one that took this into account. I was actually looking for a way to invoke isEqualToString in a similar way, but later realised that isEqual is enough for me in current scenario. However, I'm interested in a clean solution with isEqualToString, just out of curiosity. Do you know how we can achieve that?