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

前端 未结 23 1745

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 14:59

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

    In Swift 4:

    let a = "Hello, how are you?"
    a.contains("Hello")   //will return true
    
    0 讨论(0)
  • 2020-11-22 15:00
    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)

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

    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 讨论(0)
  • 2020-11-22 15:04
    -(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
      }
    
    0 讨论(0)
  • 2020-11-22 15:05
    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!");
    }
    
    0 讨论(0)
提交回复
热议问题