Skip to content

Instantly share code, notes, and snippets.

@sbooth
Created November 20, 2016 06:37
Show Gist options
  • Save sbooth/152523914308b1ff35b4e57b66180e01 to your computer and use it in GitHub Desktop.
Save sbooth/152523914308b1ff35b4e57b66180e01 to your computer and use it in GitHub Desktop.
extension String {
func snake_cased() -> String {
let re = try! NSRegularExpression(pattern: "((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))")
let s = re.stringByReplacingMatches(in: self, range: NSMakeRange(0, (self as NSString).length), withTemplate: "_$1")
return s.lowercased()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment