Skip to content

Instantly share code, notes, and snippets.

@nishabe
Last active July 1, 2019 15:50
Show Gist options
  • Save nishabe/0f3a60cd73bc2c7fcf9144d0442f674b to your computer and use it in GitHub Desktop.
Save nishabe/0f3a60cd73bc2c7fcf9144d0442f674b to your computer and use it in GitHub Desktop.
Capitalize first letter of every word in a string (Swift)
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