Last active
July 1, 2019 15:50
-
-
Save nishabe/0f3a60cd73bc2c7fcf9144d0442f674b to your computer and use it in GitHub Desktop.
Capitalize first letter of every word in a 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
extension String { | |
func capitalizingFirstLetter() -> String { | |
return prefix(1).capitalized + dropFirst() | |
} | |
func getCamelCasedString () -> String { | |
var camelCasedStringCollection = [String]() | |
let stringComponents = components(separatedBy: " ") | |
for var oneComponent in stringComponents { | |
oneComponent = oneComponent.capitalizingFirstLetter() | |
camelCasedStringCollection.append(oneComponent) | |
} | |
return camelCasedStringCollection.joined(separator: " ") | |
} | |
} | |
/* | |
Usage: | |
let test = "asda qwer edc" | |
print(test.getCamelCasedString()) | |
Output: | |
Asda Qwer Edc | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment