Skip to content

Instantly share code, notes, and snippets.

@christiancabarrocas
Created October 7, 2020 10:56
Show Gist options
  • Save christiancabarrocas/e6060b4061e17d80ff782c74157b4d4e to your computer and use it in GitHub Desktop.
Save christiancabarrocas/e6060b4061e17d80ff782c74157b4d4e to your computer and use it in GitHub Desktop.
Extension for Swift String
extension String {
func range(of string: String) -> NSRange {
return (self as NSString).range(of: string)
}
func initialLetter() -> String {
guard let letter = first else { return "" }
return String(letter)
}
static func empty() -> String {
return ""
}
func isValidEmail() -> Bool {
let regex = try! NSRegularExpression(pattern: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}", options: .caseInsensitive)
return regex.firstMatch(in: self, options: [], range: NSRange(location: 0, length: count)) != nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment