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

前端 未结 23 1768

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

    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");
    }
    

提交回复
热议问题