4

I have to make calls programmatically in my iPhone app. I have set of numbers in different countries with different formatting - braces, dots, spaces, "+" sign.

Can I simply remove all of this and left only numbers?

for example:

+1-(609) 452-8401 => 16094528401 // usa
+49(0)89.439 => 49089439 // germany
+1-(949)586-1250 => 19495861250 // los angeles, usa

Will it be correct?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
PDaria
  • 457
  • 10
  • 21
  • USE REGULAR EXPRESSION FOR IT. – Tirth Jun 07 '11 at 08:25
  • 5
    One of my favorite programming expressions: "sometimes you have a problem. then, you use regex to solve that problem. now you have 2 problems." – makdad Jun 07 '11 at 08:29
  • 1
    It's not a direct answer, but whatever you do, I HIGHLY recommend that you set up a unit test for this method that will do this work. You'll eventually find other phone numbers you want to parse properly, and unit testing will help automate the process of making sure your code is good. – makdad Jun 07 '11 at 08:30
  • If you are not experienced with regexes, try to solve the problem differently. Without thorough understanding regexes are hard to maintain, debug and read. – Nick Weaver Jun 07 '11 at 09:33

1 Answers1

3

try this:-

NSMutableString *str1=[[NSMutableString alloc] initWithString:telephoneString];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:@"(" withString:@""]];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:@")" withString:@""]];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:@"-" withString:@""]];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:@" " withString:@""]];
telephoneString = [@"tel://" stringByAppendingString:str1];
[str1 release];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telephoneString]];
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Gypsa
  • 11,230
  • 6
  • 44
  • 82