13

I have a String like "75003 Paris, France" or "Syracuse, NY 13205, USA".

I want to use the same code to remove all those numbers out of those Strings.

With expected output is "Paris, France" or "Syracuse, NY, USA".

How can I achieve that?

Pengguna
  • 4,198
  • 1
  • 24
  • 29
Kevin Science
  • 293
  • 4
  • 18

1 Answers1

27

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)
Ashish Kakkad
  • 23,020
  • 11
  • 96
  • 132
  • @KevinScience Welcome, but I have searched on stackoverflow and found one answer in objective-c, tried to do in swift and given to you :) – Ashish Kakkad Jul 10 '15 at 03:09
  • swift 4.1 var str = "75003 Paris, France" var stringWithoutDigit = (str.components(separatedBy: CharacterSet.decimalDigits)).joined(separator: "") print(stringWithoutDigit) – Pouya ghasemi Aug 14 '18 at 17:47