Filter non-digits from string

Swift 3 & 4

extension String {
    var digits: String {
        return components(separatedBy: CharacterSet.decimalDigits.inverted)
            .joined()
    }
}

Swift 5

You should be able to omit return

Also:
Read the comment from @onmyway133 for a word of caution

Leave a Comment