问题
I have a String like "75003 Paris, France"
or "Syracuse, NY 13205, USA"
.
I want to use same code to remove all those numbers out of those Strings.
How can I achieve that?
回答1:
You can do it with the NSCharacterSet
var str = "75003 Paris, France"
var stringWithoutDigit = (str.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet()) as NSArray).componentsJoinedByString("")
println(stringWithoutDigit)
Output :
Paris, France
Taken reference from : https://stackoverflow.com/a/1426819/3202193
Swift 4.x:
let str = "75003 Paris, France"
let stringWithoutDigit = (str.components(separatedBy: CharacterSet.decimalDigits)).joined(separator: "")
print(stringWithoutDigit)
来源:https://stackoverflow.com/questions/31331601/how-to-remove-numbers-from-a-string-in-swift