In Objective-C
the code to check for a substring in an NSString
is:
NSString *string = @\"hello Swift\";
NSRange textRange =[strin
Another one. Supports case and diacritic options.
Swift 3.0
struct MyString {
static func contains(_ text: String, substring: String,
ignoreCase: Bool = true,
ignoreDiacritic: Bool = true) -> Bool {
var options = NSString.CompareOptions()
if ignoreCase { _ = options.insert(NSString.CompareOptions.caseInsensitive) }
if ignoreDiacritic { _ = options.insert(NSString.CompareOptions.diacriticInsensitive) }
return text.range(of: substring, options: options) != nil
}
}
MyString.contains("Niels Bohr", substring: "Bohr") // true
Case and diacritic insensitive function available since iOS 9.
if #available(iOS 9.0, *) {
"Für Elise".localizedStandardContains("fur") // true
}