i want filter a string and get only the numbers, but the numbers with a count of characters for example 10, and the other numbers that dont meet the condition discard.
I try something like this:
let phoneAddress = "My phone number 2346172891, and my address is Florida 2234"
let withTrimming = phoneAddress.replacingOccurrences(of: "-", with: "")
.trimmingCharacters(in: CharacterSet(charactersIn: "0123456789").inverted)
let withComponents = phoneAddress.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()
But this return
withTrimming = "2346172891, and my address is Florida 2234"
withComponents = "23461728912234"
When i only want the phone number string "2346172891", i dont know how i can resolve it.
2
Answers
Use a regex such as
d{10}
:Also, consider using
NSDataDetector
.You can use Regex