Skip to content

Instantly share code, notes, and snippets.

@darhonbek
Created December 25, 2017 11:02
Show Gist options
  • Save darhonbek/b46bef4b130184f51dd2c2589cea8e43 to your computer and use it in GitHub Desktop.
Save darhonbek/b46bef4b130184f51dd2c2589cea8e43 to your computer and use it in GitHub Desktop.
Check if a string contains a substring (Swift 4)
extension String {
func contains(find: String) -> Bool{
return self.range(of: find) != nil
}
func containsIgnoringCase(find: String) -> Bool{
return self.range(of: find, options: .caseInsensitive) != nil
}
}
var value = "Hello world"
print(value.contains("Hello")) // true
print(value.contains("bo")) // false
print(value.containsIgnoringCase(find: "hello")) // true
print(value.containsIgnoringCase(find: "Hello")) // true
print(value.containsIgnoringCase(find: "bo")) // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment