Last active
March 19, 2023 16:42
-
-
Save ivanbruel/e72d938f49db64d2f5df09fb9420c1e2 to your computer and use it in GitHub Desktop.
Camel case to snake case in Swift
This file contains 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 snakeCased() -> String? { | |
let pattern = "([a-z0-9])([A-Z])" | |
let regex = try? NSRegularExpression(pattern: pattern, options: []) | |
let range = NSRange(location: 0, length: self.characters.count) | |
return regex?.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "$1_$2").lowercased() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you.