Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6585379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:39:22+00:00 2026-05-25T16:39:22+00:00

I know how to find a string in another string, that is easy. But

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:39:23+00:00Added an answer on May 25, 2026 at 4:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you want to know if a string starts with another, how would you
I know how to find a method in java using a fixed string, someClass.getMethod(foobar,
The only way I know is: find /home -xdev -samefile file1 But it's really
I know that to find all the .h files I need to use: find
Anyone know where to find a reference that describes how to output color on
This seems like a simple question, but I haven't been able to find the
(This is probably a duplicate, but I could not find it - feel free
I know there are plenty of resources on this but I'm having a tough
I know how to find what I need from XML using XPath. The syntax
I know I can find out if a variable is null in Java using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.