1

I know how to find a string in another string, that is easy. But in this case I want to find John Smith within the allProfessors string. So I figured I could just split the string and search for both parts, which works how I want:

NSString *fullName = @"John Smith";
NSArray *parts = [fullName componentsSeparatedByString:@" "];
NSString *allProfessors = @"Smith, John; Clinton, Bill; Johnson, John";
NSRange range = [[allProfessors lowercaseString] rangeOfString:[[parts objectAtIndex:0] lowercaseString]];
NSRange range2 = [[allProfessors lowercaseString] rangeOfString:[[parts objectAtIndex:1] lowercaseString]];
if(range.location != NSNotFound && range2.location != NSNotFound) {
    NSLog(@"Found");
} else {
    NSLog(@"Not Found");
}

What I want to know is, is this the BEST way to do this or is there a more preferred method to do what I want?

In addition to this, what if my fullName is longer than my allProfessors name, such as:

NSString *fullName = @"Gregory Smith";
NSString *allProfessors = @"Smith, Greg; Clinton, Bill; Johnson, John";

I still want there to be a match for Greg Smith and Gregory Smith.

3
  • 1
    Best in terms of what? Performance? Maintainability? Commented Sep 20, 2011 at 17:29
  • It would be easier if allProfessors was an NSArray instead of an NSString. Then you could transpose fullName and search thru the array. Commented Sep 20, 2011 at 17:34
  • In terms of speed and just doing things the correct way. Commented Sep 20, 2011 at 17:40

1 Answer 1

2

You could use regular expressions, which I prefer to use. See RegexKitLite.

With RegexKitLite, you could use a regular expression like (untested):

NSString *regEx = @"(?i)Smith,\\s*\\w";
NSArray *matchingStrings = [allProfessors componentsMatchedByRegex:regEx];

if ([matchingStrings count] == 0)  // not found!
{
   [...]
}
else
{
   [...]
}

Using RegexKitLite you could alternatively have used [NSString stringByMatching:(NSString*)].

You can really do a lot with regular expression. There are a ton of different functions available through Using RegexKitLite. The regular expression above should find people with the last name of Smith.

Regular Expression explained:

  • (?i) make this case insensitive
  • Smith matches last name of Smith. Obviously you could change this to anything
  • , match a comma
  • \\s* match any number of spaces (greedy)
  • \\w match a word

Also, you could use [NSString rangeOfString:options:] function like:

if ([myString rangeOfString:@"John" options:NSCaseInsensitiveSearch].location != NSNotFound &&
    [myString rangeOfString:@"Smith" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
   NSLog(@"Found");
}
else
{
   NSLog(@"Not Found");
}

Also see similar functions like [rangeOfString:options:range:locale:] so that you can do case insensitive searches and even specify a locale.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm not sure this is what you were looking for. If not, perhaps I should be asking the same question as you! :) Anyways, hopefully this is remotely helpful and provides some new information. If this is not useful, leave me a comment and I will remove it. Regular expressions are truly awesome however. They solve the dilemma of "pattern matching" nicely, but it may be overkill if you just want to see if a string contains another string. We use the NSString rangeOfString:options: where I work.
And what do you return for "Bill Smith"?
@ott +1 I really should make my solution more flexible.
Sam, I just updated my question. Can you look at the last part I just added and let me know if you think regular expressions would help in this case?
If you want to match based on whether someone has a last name or not, then ya regular expression would work great.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.