Created
July 29, 2023 13:24
-
-
Save foxicode/741bc85a8a5f6a1b7418d493b91c2f8b to your computer and use it in GitHub Desktop.
Capitalizing first letter in String (Swift)
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
| public extension String { | |
| var withFirstCapitalized: String { | |
| if isEmpty { | |
| return self | |
| } | |
| let first = substring(start: 0, length: 1) | |
| let other = count > 1 ? self.substring(start: 1, length: count - 1) : "" | |
| return "\(first.uppercased())\(other)" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment