Created
December 25, 2017 11:02
-
-
Save darhonbek/b46bef4b130184f51dd2c2589cea8e43 to your computer and use it in GitHub Desktop.
Check if a string contains a substring (Swift 4)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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