Skip to content

Instantly share code, notes, and snippets.

@eastari
Last active October 6, 2017 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eastari/5146e4df649d842ed87d54a9a6cd76f8 to your computer and use it in GitHub Desktop.
Save eastari/5146e4df649d842ed87d54a9a6cd76f8 to your computer and use it in GitHub Desktop.
Part of XCamelize
public extension String {
func camelize() -> String {
let source = removeFirstSpaces(clean(with: " ", allOf: "-", "_", "."))
if source.characters.contains(" ") {
let first = source[self.startIndex...self.index(after: startIndex)].lowercased()
let cammel = source.capitalized.replacingOccurrences(of: " ", with: "")
let rest = String(cammel.characters.dropFirst())
return "\(first.characters.first ?? self[self.startIndex])\(rest)"
} else {
let first = source[self.startIndex...self.index(after: startIndex)].lowercased()
let rest = String(source.characters.dropFirst()).lowercased()
return "\(first.characters.first ?? self[self.startIndex])\(rest)"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment