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

前端 未结 23 1751

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

I was hoping for something like:

NSString *string = @\"hello bla         


        
相关标签:
23条回答
  • 2020-11-22 15:06

    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.

    0 讨论(0)
  • 2020-11-22 15:08

    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");
    }
    
    0 讨论(0)
  • 2020-11-22 15:08

    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");
    }
    
    0 讨论(0)
  • 2020-11-22 15:13

    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");
    }
    
    0 讨论(0)
  • 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"); 
    
    0 讨论(0)
  • 2020-11-22 15:16

    try this,

    NSString *string = @"test Data";
    if ([[string lowercaseString] rangeOfString:@"data"].location == NSNotFound) 
    {
        NSLog(@"string does not contain Data");
    }   
    else 
    {
        NSLog(@"string contains data!");
    }
    
    0 讨论(0)
提交回复
热议问题