1241

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?

Sazzad Hissain Khan
  • 29,428
  • 20
  • 134
  • 192
Jonathan.
  • 51,850
  • 46
  • 174
  • 275
  • 1
    I'd like to see it added as well, but in the meantime it's relatively easy to add it as a category on NSString. – isaac Jan 11 '13 at 01:48
  • 2
    Using `if ([string rangeOfString:@"hello"] == 0) {...}` there's a type mismatch error for NSRange and int. to fix that, you should change the line to the following: `if ([string rangeOfString:@"hello"].length == 0) {...}` – Neeku Nov 11 '13 at 16:14
  • 1
    iOS 8 adds containsString: and here is a minimally invasive way to add iOS 7 support http://petersteinberger.com/blog/2014/retrofitting-containsstring-on-ios-7/ – Steve Moser Aug 06 '14 at 03:18
  • 2
    I've been an iOS developer since the beginning and I constantly revisit this post for a quick copy paste. I can't seem to memorize this one. Most visited stackoverflow post in my history. – VaporwareWolf May 04 '16 at 19:13

23 Answers23

2466
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)

Dave DeLong
  • 239,073
  • 58
  • 443
  • 495
  • 287
    To make a case insensitive search use "if ([string rangeOfString:@"bla" options:NSCaseInsensitiveSearch].location != NSNotFound)" – Vanja Mar 28 '12 at 07:28
  • 1
    @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. – dherrin79 Sep 05 '14 at 14:11
  • Why does the compiler don't say anything if my deployment target is iOS7 and I use containsString? – Ricardo Oct 23 '15 at 11:05
  • 3
    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]". – Scott Kohlert Mar 28 '16 at 19:53
  • @ScottKohlert Careful, `[stringToSearch localizedStandardContainsString:string]` was introduced in **iOS9**. – Jonathan F. Jul 05 '16 at 08:06
  • 1
    Note that the expression `[string rangeOfString:@"bla"].location != NSNotFound` will be **true** when string is `nil`! – funroll Oct 07 '16 at 20:12
  • 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@@:"); }` – Kamil.S Apr 02 '17 at 08:27
162

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

Cœur
  • 32,421
  • 21
  • 173
  • 232
P i
  • 25,182
  • 33
  • 133
  • 229
  • 13
    +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. https://github.com/peterdeweese/es_ios_utils – Peter DeWeese Jul 21 '11 at 15:21
  • 4
    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 – Islam Q. Jan 03 '15 at 19:32
  • 7
    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 – Daniel Galasko Feb 17 '15 at 10:38
56

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");
}
Pang
  • 8,605
  • 144
  • 77
  • 113
Lukas
  • 2,868
  • 1
  • 14
  • 9
41
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!");
}
P.J.Radadiya
  • 1,372
  • 10
  • 16
AJS
  • 1,383
  • 11
  • 17
25

With iOS 8 and Swift, we can use localizedCaseInsensitiveContainsString method

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

 string.localizedCaseInsensitiveContainsString(substring) // true
Rishil Patel
  • 1,938
  • 3
  • 11
  • 28
Govind
  • 2,252
  • 31
  • 40
13

So personally I really hate NSNotFound but understand its necessity.

But some people may not understand the complexities of comparing against NSNotFound

For example, this code:

- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
    if([string rangeOfString:otherString].location != NSNotFound)
        return YES;
    else
        return NO;
}

has its problems:

1) Obviously if otherString = nil this code will crash. a simple test would be:

NSLog(@"does string contain string - %@", [self doesString:@"hey" containString:nil] ? @"YES": @"NO");

results in !! CRASH !!

2) What is not so obvious to someone new to objective-c is that the same code will NOT crash when string = nil. For example, this code:

NSLog(@"does string contain string - %@", [self doesString:nil containString:@"hey"] ? @"YES": @"NO");

and this code:

NSLog(@"does string contain string - %@", [self doesString:nil containString:nil] ? @"YES": @"NO");

will both result in

does string contains string - YES

Which is clearly NOT what you want.

So the better solution that I believe works is to use the fact that rangeOfString returns the length of 0 so then a better more reliable code is this:

- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
    if(otherString && [string rangeOfString:otherString].length)
        return YES;
    else
        return NO;
}

OR SIMPLY:

- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
    return (otherString && [string rangeOfString:otherString].length);
}

which will for cases 1 and 2 will return

does string contains string - NO

That's my 2 cents ;-)

Please check out my Gist for more helpful code.

ucangetit
  • 2,445
  • 24
  • 19
12

An improved version of P i's solution, a category on NSString, that not only will tell, if a string is found within another string, but also takes a range by reference, is:

@interface NSString (Contains)
-(BOOL)containsString: (NSString*)substring
              atRange:(NSRange*)range;

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

@implementation NSString (Contains)

-(BOOL)containsString:(NSString *)substring
              atRange:(NSRange *)range{

    NSRange r = [self rangeOfString : substring];
    BOOL found = ( r.location != NSNotFound );
    if (range != NULL) *range = r;
    return found;
}

-(BOOL)containsString:(NSString *)substring
{
    return [self containsString:substring
                        atRange:NULL];
}

@end

Use it like:

NSString *string = @"Hello, World!";

//If you only want to ensure a string contains a certain substring
if ([string containsString:@"ello" atRange:NULL]) {
    NSLog(@"YES");
}

// Or simply
if ([string containsString:@"ello"]) {
    NSLog(@"YES");
}

//If you also want to know substring's range
NSRange range;
if ([string containsString:@"ello" atRange:&range]) {
    NSLog(@"%@", NSStringFromRange(range));
}
Community
  • 1
  • 1
vikingosegundo
  • 51,126
  • 14
  • 131
  • 172
8

Here is a copy-and-paste function snippet:

-(BOOL)Contains:(NSString *)StrSearchTerm on:(NSString *)StrText
{
    return [StrText rangeOfString:StrSearchTerm 
        options:NSCaseInsensitiveSearch].location != NSNotFound;
}
Durai Amuthan.H
  • 28,889
  • 6
  • 148
  • 223
7

Oneliner (Smaller amount of code. DRY, as you have only one NSLog):

NSString *string = @"hello bla bla";
NSLog(@"String %@", ([string rangeOfString:@"bla"].location == NSNotFound) ? @"not found" : @"cotains bla"); 
jerik
  • 5,322
  • 7
  • 38
  • 75
6
NSString *categoryString = @"Holiday Event";
if([categoryString rangeOfString:@"Holiday"].location == NSNotFound)
{
    //categoryString does not contains Holiday
}
else
{
    //categoryString contains Holiday
}
spenibus
  • 4,079
  • 11
  • 23
  • 33
Ada
  • 466
  • 8
  • 13
5

try this,

NSString *string = @"test Data";
if ([[string lowercaseString] rangeOfString:@"data"].location == NSNotFound) 
{
    NSLog(@"string does not contain Data");
}   
else 
{
    NSLog(@"string contains data!");
}
Simon Hughes
  • 3,425
  • 3
  • 20
  • 44
Pooja Patel
  • 616
  • 8
  • 11
5

Best solution. As simple as this! If you want to find a word or part of the string. You can use this code. In this example we are going to check if the value of word contains "acter".

NSString *word =@"find a word or character here";
if ([word containsString:@"acter"]){
    NSLog(@"It contains acter");
} else {
     NSLog (@"It does not contain acter");
}
olyv
  • 3,265
  • 3
  • 31
  • 66
handiansom
  • 702
  • 11
  • 27
5

In Swift 4:

let a = "Hello, how are you?"
a.contains("Hello")   //will return true
pirho
  • 8,805
  • 12
  • 34
  • 44
garg
  • 2,344
  • 20
  • 20
4

If you need this once write:

NSString *stringToSearchThrough = @"-rangeOfString method finds and returns the range of the first occurrence of a given string within the receiver.";
BOOL contains = [stringToSearchThrough rangeOfString:@"occurence of a given string"].location != NSNotFound;
Nikolay Shubenkov
  • 2,988
  • 1
  • 26
  • 31
4

In case of swift, this can be used

let string = "Package #23"
if string.containsString("Package #") {
    //String contains substring
}
else {
    //String does not contain substring
}
Sreedeepkesav M S
  • 997
  • 11
  • 15
2

If do not bother about case-sensitive string. Try this once.

NSString *string  = @"Hello World!";

if([string rangeOfString:@"hello" options:NSCaseInsensitiveSearch].location !=NSNotFound)
{
    NSLog(@"found");
}
else
{
    NSLog(@"not found");
}
P.J.Radadiya
  • 1,372
  • 10
  • 16
Abhijit
  • 173
  • 1
  • 7
1

Please use this code

NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) 
{
    NSLog(@"string does not contain bla");
} 
else 
{  
    NSLog(@"string contains bla!");
}
Simon Hughes
  • 3,425
  • 3
  • 20
  • 44
Rohit suvagiya
  • 1,001
  • 2
  • 12
  • 40
1

Use the option NSCaseInsensitiveSearch with rangeOfString:options:

NSString *me = @"toBe" ;
NSString *target = @"abcdetobe" ;
NSRange range = [target  rangeOfString: me options: NSCaseInsensitiveSearch];
NSLog(@"found: %@", (range.location != NSNotFound) ? @"Yes" : @"No");
if (range.location != NSNotFound) {
// your code
}

Output result is found:Yes

The options can be "or'ed" together and include:

NSCaseInsensitiveSearch NSLiteralSearch NSBackwardsSearch and more

0

First string contain or not second string,

NSString *first = @"Banana";
NSString *second = @"BananaMilk";
NSRange range = [first rangeOfString:second options:NSCaseInsensitiveSearch];

if (range.length > 0) {
    NSLog(@"Detected");
}
else {
    NSLog(@"Not detected");
}
Pang
  • 8,605
  • 144
  • 77
  • 113
Kamani Jasmin
  • 575
  • 6
  • 10
0

Swift 4 And Above

let str = "Hello iam midhun"

if str.contains("iam") {
  //contains substring
}
else {
  //doesn't contain substring
}

Objective-C

NSString *stringData = @"Hello iam midhun";

if ([stringData containsString:@"iam"]) {
    //contains substring
}
else {
    //doesn't contain substring
}
Luckabo
  • 62
  • 4
midhun p
  • 1,351
  • 11
  • 20
0

Try this:

Swift 4.1 , 4.2:

let stringData = "Black board"

//swift quick way and case sensitive
if stringData.contains("bla") {
    print("data contains string");
}

//case sensitive
if stringData.range(of: "bla",options: .caseInsensitive) != nil {
    print("data contains string");
}else {
    print("data does not contains string");
}

For Objective-C:

NSString *stringData = @"Black board";

//Quick way and case sensitive
if ([stringData containsString:@"bla"]) {
    NSLog(@"data contains string");
}

//Case Insensitive
if ([stringData rangeOfString:@"bla" options:NSCaseInsensitiveSearch].location != NSNotFound) {
   NSLog(@"data contains string");
}else {
   NSLog(@"data does not contain string");
}
Amir.n3t
  • 1,594
  • 12
  • 25
-1
-(Bool)checkIf:(String)parentString containsSubstring:(String)checkString {
    NSRange textRange =[parentString rangeOfString:checkString];
    return textRange.location != NSNotFound // returns true if parent string contains substring else returns false
  }
iOS Nepal
  • 101
  • 1
  • 10
  • 1
    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. – Aleksey Potapov Dec 06 '19 at 21:59
-2

If certain position of the string is needed, this code comes to place in Swift 3.0:

let string = "This is my string"
let substring = "my"

let position = string.range(of: substring)?.lowerBound
pedrouan
  • 11,915
  • 2
  • 55
  • 71