我有一个字符串,例如"75003 Paris, France"
或"Syracuse, NY 13205, USA"
。
我想使用同样的代码将这些字符串中的所有数字都删除。
期望的输出是"Paris, France"
或"Syracuse, NY, USA"
。
我该如何实现?
NSCharacterSet
来实现。var str = "75003 Paris, France"
var stringWithoutDigit = (str.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet()) as NSArray).componentsJoinedByString("")
println(stringWithoutDigit)
输出:
Paris, France
参考来源:https://dev59.com/OXNA5IYBdhLWcg3wAI3d#1426819
Swift 4.x:
let str = "75003 Paris, France"
let stringWithoutDigit = (str.components(separatedBy: CharacterSet.decimalDigits)).joined(separator: "")
print(stringWithoutDigit)