0

I am developing an iphone app where users input a few words and I then need to look them up (if they exist basically) in a local hard coded long array of words (my case is more complex to explain, but lets say a user is entering 5 names of cities and I need to look up my array of cities and let him know which onces exist).

What would be the best way to go about this problem? especially since some of the names may be found in a sentence format (for example, someone can enter: "the city of paris", but I have "Paris", so its still good).

Is there any good 3rd part lib for that maybe? I can definitely try to do that manually, but I am sure there are some better solutions.

Thanks.

TommyG
  • 4,091
  • 10
  • 39
  • 66

2 Answers2

4

I'd use NSPredicate:

NSString *searchText = @"paris";  //user's input 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self LIKE[c] %@", searchText]; 
NSArray *results = [yourHardcodedArrayOfCities filteredArrayUsingPredicate:predicate];

This will fill *results with any matching results from your main array. The [c] in the predicate specifies case insensitivity, so Paris will match paris or pArIs.

Edited to add

Instead of using LIKE in the predicate (as shown in my original answer), it is easier to use CONTAINS to find matches if your search text is a sentence. So, you could use:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS[c] %@", searchText];

Same result, but a bit easier. This way, if the search string is "New York City" or "City of New York" it will still match on your hardcoded entry of "New York".

Here is an Apple doc on using predicates which you might find useful: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html

Jim
  • 2,308
  • 1
  • 19
  • 40
  • Thanks, changed my answer to use the CONTAINS clause of NSPredicate - a bit cleaner than using LIKE in my first answer – Jim Nov 01 '11 at 17:45
  • hi Jim - could you tell how should I modify this code if I want to search "paris" within a string rather than an array? (so I have a long string and somewhere inside it there is the city name). – TommyG Nov 02 '11 at 19:52
  • Tommy - see http://stackoverflow.com/questions/2753956/string-contains-string-in-objective-c for a good answer to this question (sorry for the late response - I was on vaca) – Jim Nov 08 '11 at 18:24
0

Using std::string's find

userInput and predefinedPhrase are both std::string

for userInput in userInputList
  for predefinedPhrase in your predefinedPhraseSet
    test userInput.find(predefinedPhrase);

I first tried to avoid iterating over your predefinedPhraseSet, since it might be large.
But I guess there's no other way, the other solution (with predicate) seems to do that as well.

eugene
  • 33,301
  • 47
  • 188
  • 382
  • What about if you are searching for city names which are more than one word? If you split on whitespace, won't a user searching for "New York" result in looking at the predefinedSet for two separate queries - "New" and "York"? – Jim Nov 01 '11 at 17:47