Created
April 8, 2017 22:47
-
-
Save hcrub/e40284ebf8fb136823cf433514969147 to your computer and use it in GitHub Desktop.
Extension for checking if a String is capitalized in Swift 3.0+.
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 isCapitalized() -> Bool { | |
guard self.characters.count > 0 else { | |
return false | |
} | |
return (CharacterSet.uppercaseLetters as NSCharacterSet).characterIsMember(String(self.characters.first!).utf16[String.UTF16Index(0)]); | |
} | |
} | |
// Test | |
let token = "Hello World" | |
let isCapitalized = token.isCapitalized() | |
print("\"\(token)\" is Capitalized? \(isCapitalized)") // "Hello World" is Capitalized? true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment