Reputation: 598
I have strings with stores phone numbers with formats:
Than i need to make an algorithm to add 9090/90 before de 9999-9999 to get the results:
I know how to do it using an algorithm, but i need to know if i can do with a easiest and better way (like use regex).
Upvotes: 1
Views: 279
Reputation: 726589
You can do it with a regular expression match:
let str = "+55 (14) 99999-9999"
let optRange = str.rangeOfString("[(]14[)]", options: .RegularExpressionSearch)
if let range = optRange? {
let prefix = Range<String.Index>(start:str.startIndex, end:range.startIndex)
let suffix = Range<String.Index>(start:range.startIndex, end:str.endIndex)
let res = "\(str.substringWithRange(prefix))90 \(str.substringWithRange(suffix))"
println(res)
}
This produces
+55 90 (14) 99999-9999
You would need an else
branch to deal with the situation when the (14)
substring is not found.
Upvotes: 2