Mashhadi
Mashhadi

Reputation: 3024

How to Search 2nd word in a string of array using UISearchBar?

I have to search a word in an array using UISearchBar. for example i have "Chicken Salad" string in an array at index 2. now if want to search it using last word "Salad" then what should i do? for the time i am using this code and it is working good but if you search from first word.

(appDelegate.isAirlineSelection == YES) {
            for (int i = 0; i < [menuArray count]; i++) {
                if ([[[[menuArray objectAtIndex:i] itemName] uppercaseString] hasPrefix:search]||[[[[menuArray objectAtIndex:i] itemName] uppercaseString] hasSuffix:search]) {
                    [tableMenuArray addObject:[menuArray objectAtIndex:i]];
                    NSLog(@"Found");
                }
            }

Kindly help me

Upvotes: 0

Views: 345

Answers (2)

Ankit Gupta
Ankit Gupta

Reputation: 958

First you should break your array name in two parts and create two arrays, then you can easily use search bar for second array and can find exact result ,what do you want.

for (NSString *name in lastnames)
    {

            NSComparisonResult result = [name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
            {
                [searchedNames addObject:name];
            }

    }

Upvotes: 0

Adil Soomro
Adil Soomro

Reputation: 37729

try this way, It'll search even between the words:

for(NSString *name in menuArray)        
{
    if ([name rangeOfString:search options:NSLiteralSearch|NSCaseInsensitiveSearch].location != NSNotFound)
        [tableMenuArray addObject:name];
            NSLog(@"Found");

}

Upvotes: 1

Related Questions