Created
October 7, 2018 02:55
-
-
Save trilliwon/3db6fc56a1ec0d5acde4590750e35d64 to your computer and use it in GitHub Desktop.
is alpha?
This file contains hidden or 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
| func isAlpha(char: Character) -> Bool { | |
| switch char { | |
| case "a"..."z": | |
| return true | |
| case "A"..."Z": | |
| return true | |
| default: | |
| return false | |
| } | |
| } | |
| func isAlpha2(char: String) -> Bool { | |
| return charset(NSCharacterSet.alphanumericCharacterSet(), containsCharacter: char) | |
| } | |
| func charset(cset:NSCharacterSet, containsCharacter s:String) -> Bool { | |
| let ix = s.startIndex | |
| let ix2 = s.endIndex | |
| let result = s.rangeOfCharacterFromSet(cset, options: [], range: ix..<ix2) | |
| return result != nil | |
| } | |
| func isAlpha3(char: String) -> Bool { | |
| let regex = try! NSRegularExpression(pattern: "[:alpha:]", options: []) | |
| return regex.firstMatchInString(char, options: [], range: NSMakeRange(0, char.characters.count)) != nil | |
| } | |
| isAlpha("a") | |
| isAlpha("B") | |
| isAlpha(" ") | |
| isAlpha2("a") | |
| isAlpha2("B") | |
| isAlpha2("?") | |
| isAlpha3("a") | |
| isAlpha3("B") | |
| isAlpha3("?") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment