Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove special characters from the string

Tags:

ios

swift

I am trying to use an iOS app to dial a number. The problem is that the number is in the following format:

po placeAnnotation.mapItem.phoneNumber!
"‎+1 (832) 831-6486"

I want to get rid of some special characters and I want the following:

832-831-6486

I used the following code but it did not remove anything:

let charactersToRemove = CharacterSet(charactersIn: "()+-")
var telephone = placeAnnotation.mapItem.phoneNumber?.trimmingCharacters(in: charactersToRemove)

Any ideas?

like image 683
john doe Avatar asked Nov 27 '22 16:11

john doe


1 Answers

placeAnnotation.mapItem.phoneNumber!.components(separatedBy: CharacterSet.decimalDigits.inverted)
    .joined()

Here you go!

I tested and works well.enter image description here

like image 146
Rob C Avatar answered Dec 05 '22 13:12

Rob C