【问题标题】:phone number format for iPhoneiPhone的电话号码格式
【发布时间】:2011-06-07 08:22:32
【问题描述】:

我必须在我的 iPhone 应用程序中以编程方式拨打电话。 我在不同国家有一组不同格式的数字 - 大括号、点、空格、“+”号。

我可以简单地删除所有这些并只留下数字吗?

例如:

+1-(609) 452-8401 => 16094528401 // 美国
+49(0)89.439 => 49089439 // 德国
+1-(949)586-1250 => 19495861250 // 美国洛杉矶

会是正确的吗?

【问题讨论】:

  • 使用正则表达式。
  • 我最喜欢的编程表达之一:“有时你有一个问题。然后,你使用正则表达式来解决这个问题。现在你有 2 个问题。”
  • 这不是一个直接的答案,但无论你做什么,我强烈建议你为此方法设置一个单元测试来完成这项工作。您最终会找到您想要正确解析的其他电话号码,而单元测试将有助于自动化确保您的代码良好的过程。
  • 如果您没有使用正则表达式的经验,请尝试以不同的方式解决问题。没有透彻的理解正则表达式很难维护、调试和阅读。

标签: ios iphone formatting phone-number phone-call


【解决方案1】:

试试这个:-

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]];

【讨论】:

    猜你喜欢
    • 2014-04-01
    • 2012-08-11
    • 2012-07-16
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    相关资源
    最近更新 更多