//第一种:
extension RangeReplaceableCollection where Self: StringProtocol {
var digits: Self {
return filter({ $0.isNumber })
}
}
//使用
"abc12345".digits // "12345"
//第二种:
extension RangeReplaceableCollection where Self: StringProtocol {
mutating func removeAllNonNumeric() {
removeAll { !$0.isNumber }
}
}
//使用:
var str = "123abc0"
str.removeAllNonNumeric()
print(str) //"1230"
网友评论