Skip to content

Instantly share code, notes, and snippets.

@jorgenisaksson
Last active April 3, 2017 08:45
Show Gist options
  • Save jorgenisaksson/f105784e35501b3136301f764874ce29 to your computer and use it in GitHub Desktop.
Save jorgenisaksson/f105784e35501b3136301f764874ce29 to your computer and use it in GitHub Desktop.
Uppercase the first character of a Swift string
import Foundation
extension String {
func firstCharacterUpperCase() -> String {
if let firstCharacter = characters.first, characters.count > 0 {
return replacingCharacters(in: startIndex ..< index(after: startIndex), with: String(firstCharacter).uppercased())
}
return self
}
}
// use
let myString = "hello world!"
print(myString.firstCharacterUpperCase()) // Hello world!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment