How to remove numbers from a String in Swift [closed]

半世苍凉 提交于 2019-12-18 15:47:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!