How do I check if a string contains another string in Swift?

后端 未结 27 3659
天命终不由人
天命终不由人 2020-11-22 12:32

In Objective-C the code to check for a substring in an NSString is:

NSString *string = @\"hello Swift\";
NSRange textRange =[strin         


        
27条回答
  •  轮回少年
    2020-11-22 13:17

    // Search string exist in employee name finding.
    var empName:NSString! = employeeDetails[filterKeyString] as NSString
    
    Case sensitve search.
    let rangeOfSearchString:NSRange! = empName.rangeOfString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch)
    
    // Not found.
    if rangeOfSearchString.location != Foundation.NSNotFound
    {
        // search string not found in employee name.
    }
    // Found
    else
    {
        // search string found in employee name.
    }
    

提交回复
热议问题