Question

I came across a question that had an example using rangeOfString. How ever the first thing that came to mind was NSPredicate.

I have different strings that return words separated by sentences. For example I have one that returns "Male, Female".

What is the most efficient way to search either "Male" or "Female". I'd like to perform some actions if the word happens to be part of the sentence and if it doesn't.

NSDictionary with stored words separated by commas. I use different keys to grab specific bunch of words. Below I use the "selectedGenders" key which returns "Male, Female":

    if ([combinedRefinementSelection valueForKey:@"selectedGenders"]) {

        NSString *selectedGenders = [combinedRefinementSelection valueForKey:@"selectedGenders"];

        // Show string in label so customer knows how their clothes items will be filtered
        [[_thisController chosenGender] setText:selectedGenders];

    }

I simply want to search selectedGenders and find out if Male or Female is part of the string.

Was it helpful?

Solution

As you said, rangeOfString works just fine.

NSString* sentence = @"Male, Female";

if ([sentence rangeOfString:@"Male"].location != NSNotFound)
{
   NSLog(@"Male is found");
}

if ([sentence rangeOfString:@"Female"].location != NSNotFound)
{
   NSLog(@"Female is found");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top