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

前端 未结 23 2089

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

提交回复
热议问题