Skip to content

Instantly share code, notes, and snippets.

@hcrub
Created April 8, 2017 22:47
Show Gist options
  • Save hcrub/e40284ebf8fb136823cf433514969147 to your computer and use it in GitHub Desktop.
Save hcrub/e40284ebf8fb136823cf433514969147 to your computer and use it in GitHub Desktop.
Extension for checking if a String is capitalized in Swift 3.0+.
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