It probably seems to be a noob question for a lot of members, but I looked for this on the forum and I was unable to find an answer :
how can I check in a string contains another string ?
I tried something like this :
Quote:
if ([item.title compare:searchBar.text]==NSOrderedSame) || ([item.subtitle compare:searchBar.text]==NSOrderedSame)
But the problem is this always returns TRUE
I looked in the Xcode doc too, but either I don't completely understand it or I'm doing something wrong but I still cannot get the result I want.
I'm trying to use this to check if the text typed in a searchBar corresponds to the title or subtitle attributes of an objects array.
Firstly, please post the exact code you're using rather than "something like" it.
Secondly, are you checking that all the strings you are referencing are valid? In particularly that none of them are nil references?
Thanks to you that works. Indeed that's because my item.subtitle are nil actually.
So I ask 1 more question : why did that return yes if I was looking for "aText" in a nil string ?
Thanks for your answer
Last edited by keyneosoft; 12-09-2009 at 05:03 AM.
So I ask 1 more question : why did that return yes if I was looking for "aText" in a nil string ?
If you check the docs you'll see that behaviour during a comparison with a 'nil' is "undefined", so it's just how it happens to be rather than a reliable design decision.
__________________
Visit Mr Jack Games for my blog and more about my games
So I ask 1 more question : why did that return yes if I was looking for "aText" in a nil string ?
If you send a message to a nil object (in this case, item.subtitle) you get a nil returned. And since nil is basically 0, and NSOrderedSame is defined as 0, the == test returns true.
__________________ SimCap - Simple iPhone and iPad Simulator screen capture
If you send a message to a nil object (in this case, item.subtitle) you get a nil returned. And since nil is basically 0, and NSOrderedSame is defined as 0, the == test returns true.
Ah yes, not reading correctly I'd though he was passing a nil object as the compare argument.
__________________
Visit Mr Jack Games for my blog and more about my games
Ah yes, not reading correctly I'd though he was passing a nil object as the compare argument.
Ok thanks for this explanation.
I have 1 more question :
My comparison now almost works correctly. But I have a problem with it, as it seems to be case-sensitive. I tried to use
But it seems to return false if the string I type in the searchBar is not exactly the same as the item.title.
You're French, no? Are you comparing strings that contain accented characters? These may not be correctly compared unless you use localizedCaseInsensitiveCompare instead.
Otherwise, could you post your code, and the specific strings that you're having problems with?
__________________
Visit Mr Jack Games for my blog and more about my games
You're French, no? Are you comparing strings that contain accented characters? These may not be correctly compared unless you use localizedCaseInsensitiveCompare instead.
Otherwise, could you post your code, and the specific strings that you're having problems with?
Yes I'm french, but I think that doesn't come from this.
I cannot post the specific strings because they come from a csv of french towns that I parse.
The strings are composed an address (including the name of the city) : for instance 24 RUE DE LA LIBERTE LILLE and so on.
Every string is in uppercase, to avoid the accented characters case.
On the app, I want to find the objects that correspond to a search I'd type in the searchBar.
My test is the following one :
Quote:
for (POI *item in allPOIs) {
int index = 0;
if([searchBar.text isEqualToString:item.title] || [searchBar.text isEqualToString:item.subtitle]
|| [item.title hasPrefix:searchBar.text] || [item.subtitle hasPrefix:searchBar.text]
|| [item.title hasSuffix:searchBar.text] || [item.subtitle hasSuffix:searchBar.text]
|| ([item.title caseInsensitiveCompare:searchBar.text]==NSOrderedSame))
{
[searchTempPOIs addObject:item];
}
index++;
}
where searchTempPOIs is an array of POI.
Do you see where is my mistake ?
Thanks for your answers anyway
The strings are composed an address (including the name of the city) : for instance 24 RUE DE LA LIBERTE LILLE and so on.
Every string is in uppercase, to avoid the accented characters case.
On the app, I want to find the objects that correspond to a search I'd type in the searchBar.
Hang on, you're making a search bar? Are you looking for exact comparisons or substrings? The code you've got should match '24 rue de la liberte lille' for your example, but it won't match 'rue' or 'rue de la liberte'.
Have a look at rangeOfString: instead, it will search for substring matches anywhere in a string.
Even so, this is a very inefficient way of searching, unless you have a small search space you may find it gets impractically slow.
__________________
Visit Mr Jack Games for my blog and more about my games