How do I check if a string contains another string in Objective-C?

571,866

Solution 1

NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) {
  NSLog(@"string does not contain bla");
} else {
  NSLog(@"string contains bla!");
}

The key is noticing that rangeOfString: returns an NSRange struct, and the documentation says that it returns the struct {NSNotFound, 0} if the "haystack" does not contain the "needle".


And if you're on iOS 8 or OS X Yosemite, you can now do: (*NOTE: This WILL crash your app if this code is called on an iOS7 device).

NSString *string = @"hello bla blah";
if ([string containsString:@"bla"]) {
  NSLog(@"string contains bla!");
} else {
  NSLog(@"string does not contain bla");
}

(This is also how it would work in Swift)

👍

Solution 2

For iOS 8.0+ and macOS 10.10+, you can use NSString's native containsString:.

For older versions of iOS and macOS, you can create your own (obsolete) category for NSString:

@interface NSString ( SubstringSearch )
    - (BOOL)containsString:(NSString *)substring;
@end

// - - - - 

@implementation NSString ( SubstringSearch )

- (BOOL)containsString:(NSString *)substring
{    
    NSRange range = [self rangeOfString : substring];
    BOOL found = ( range.location != NSNotFound );
    return found;
}

@end

Note: Observe Daniel Galasko's comment below regarding naming

Solution 3

Since this seems to be a high-ranking result in Google, I want to add this:

iOS 8 and OS X 10.10 add the containsString: method to NSString. An updated version of Dave DeLong's example for those systems:

NSString *string = @"hello bla bla";
if ([string containsString:@"bla"]) {
    NSLog(@"string contains bla!");
} else {
    NSLog(@"string does not contain bla");
}

Solution 4

NSString *myString = @"hello bla bla";
NSRange rangeValue = [myString rangeOfString:@"hello" options:NSCaseInsensitiveSearch];

if (rangeValue.length > 0)
{
    NSLog(@"string contains hello");
} 
else 
{
    NSLog(@"string does not contain hello!");
}

//You can alternatively use following too :

if (rangeValue.location == NSNotFound) 
{
    NSLog(@"string does not contain hello");
} 
else 
{
    NSLog(@"string contains hello!");
}

Solution 5

With iOS 8 and Swift, we can use localizedCaseInsensitiveContainsString method

 let string: NSString = "Café"
 let substring: NSString = "É"

 string.localizedCaseInsensitiveContainsString(substring) // true
Share:
571,866
Jonathan.
Author by

Jonathan.

I'm a 25 year old Software engineer in London, proudly working on the Elfin Market iOS app. I've made a few tweaks for jailbroken iOS and some other stuff.

Updated on December 07, 2020

Comments

  • Jonathan.
    Jonathan. over 3 years

    How can I check if a string (NSString) contains another smaller string?

    I was hoping for something like:

    NSString *string = @"hello bla bla";
    NSLog(@"%d",[string containsSubstring:@"hello"]);
    

    But the closest I could find was:

    if ([string rangeOfString:@"hello"] == 0) {
        NSLog(@"sub string doesnt exist");
    } 
    else {
        NSLog(@"exists");
    }
    

    Anyway, is that the best way to find if a string contains another string?

  • Peter DeWeese
    Peter DeWeese almost 13 years
    +1 for clearer resulting code and reusability. I turned it into the one liner return [self rangeOfString:substring].location != NSNotFound; and included it in my refactoring library, es_ios_utils. github.com/peterdeweese/es_ios_utils
  • Vanja
    Vanja over 12 years
    To make a case insensitive search use "if ([string rangeOfString:@"bla" options:NSCaseInsensitiveSearch].location != NSNotFound)"
  • Lucas
    Lucas almost 10 years
    This is good. No idea why they didn't have this method for ios 7
  • dherrin79
    dherrin79 almost 10 years
    @Dave DeLong I was just going to mention a Category that I created for this purpose before I read your edit to the answer! Since I am primarily a c# developer I am glad that they added a contains method to NSSTring.
  • Islam
    Islam over 9 years
    Looks like Apple likes your idea and added this feature in iOS 8 and OSx 10.10 (Yosemite) as @DaveDeLong mentioned in his answer. +1
  • Daniel Galasko
    Daniel Galasko over 9 years
    The cardinal rule for obj-c categories is to prefix the method name with your 3 letter module prefix. This is the perfect example since it now conflicts with the iOS 7 and 10.10 release
  • Durai Amuthan.H
    Durai Amuthan.H almost 9 years
    May I know why I am downvoted ? It's a working code snippet
  • Hemang
    Hemang almost 9 years
    @Lucas, because Swift launched with iOS 8.0. but with swift you can still support the devices with iOS 7.
  • Ricardo
    Ricardo over 8 years
    Why does the compiler don't say anything if my deployment target is iOS7 and I use containsString?
  • Scott Kohlert
    Scott Kohlert over 8 years
    And to further the point made by @Vanja : if you are going to use the [string containsString] shortcut code that was introduced in iOS 8/Yosemite, you can use the following code for a case insensitive string: "[stringToSearch localizedCaseInsensitiveContainsString:string]", and this one if you want to do a case and diacritic insensitive search: "[stringToSearch localizedStandardContainsString:string]".
  • Jonathan F.
    Jonathan F. almost 8 years
    @ScottKohlert Careful, [stringToSearch localizedStandardContainsString:string] was introduced in iOS9.
  • funroll
    funroll over 7 years
    Note that the expression [string rangeOfString:@"bla"].location != NSNotFound will be true when string is nil!
  • Kamil.S
    Kamil.S about 7 years
    To keep iOS7 support one can add containsString: dynamically, for iOS8+ this code won't do anything and genuine containsString: will be used. - (void)setupContainsString { SEL containsStringSelector = @selector(containsString:); IMP containsStringIMP = imp_implementationWithBlock(^(id _self, NSString *string) {\ NSRange range = [_self rangeOfString:string]; return range.length != 0; }); //The method is added only if class has no implementation of it class_addMethod([NSString class], containsStringSelector, containsStringIMP, "c@@:"); }
  • Aleksey Potapov
    Aleksey Potapov over 4 years
    In Objective-C we have 'NSString` type. We don't have String type. Secondly, we have all the objects. So as an argument you should pass NSString. I vote for deletion due to the poor quality of an answer under highly active question.
  • iOS Nepal
    iOS Nepal over 2 years
    @AlekseyPotapov the idea is to give the logic rather than make the copy paste ready code.
  • PJP
    PJP over 2 years
    Please read "How to Answer" and "Explaining entirely code-based answers". It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code.