13 lines
372 B
Swift
13 lines
372 B
Swift
import Foundation
|
|
|
|
extension String {
|
|
public func camelCaseToSnakeCase() -> String {
|
|
let pattern = "([a-z0-9])([A-Z])"
|
|
let regex = try! NSRegularExpression(pattern: pattern, options: [])
|
|
let range = NSRange(location: 0, length: self.count)
|
|
return regex.stringByReplacingMatches(
|
|
in: self, options: [], range: range, withTemplate: "$1_$2"
|
|
).lowercased()
|
|
}
|
|
}
|