Last active
April 3, 2017 08:45
-
-
Save jorgenisaksson/f105784e35501b3136301f764874ce29 to your computer and use it in GitHub Desktop.
Uppercase the first character of a Swift string
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
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