将 iPhone 电话号码格式转换为“可拨号”格式细绳
我正在构建一个 iPhone 应用程序,它从地址簿中读取电话号码并拨打它(当然还有其他一些东西......:))。当我从 AB 加载电话号码时,它们采用以下格式:“1 (111) 111-1111”,在使用此功能时不可“拨打”:
fullNumber = [NSString stringWithFormat:@"%@%@", @"tel:", phoneNum];
为什么会发生这种情况?解决这个问题的最佳方法是什么?如何将电话号码转换为一串数字(不含空格或“-”)?
谢谢。
I am building an iphone app that reads a phone number from the address book and dial it (with some other things of course...:)). When I load the phone numbers from the AB they are in this following format: "1 (111) 111-1111" which is not "dial-able" when using this:
fullNumber = [NSString stringWithFormat:@"%@%@", @"tel:", phoneNum];
Why is that happening? What would be the best way to approach this? How could I convert the phone number into a string of digits (with no spaces or "-")?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将删除所有非数字字符:
This will strip out all non-numeric characters:
要回答有关从字符串中删除特定字符的问题...请查看 NSString类参考具体方法
stringByReplacingOccurrencesOfString:withString:
您可以这样做
显然不是最有效的...但它应该让您了解该方法以及如何删除特定字符。
在下面的帖子中可以看到涵盖除数字之外的任何字符的另一个选项,这可能是解决您问题的更好方法。
从 NSString 中删除除数字之外的所有内容
To answer your question about removing specific characters from a string... Take a look at the NSString Class Reference specifcally the method
stringByReplacingOccurrencesOfString:withString:
You could do
Obviously not the most efficient... but it should give you an idea of the method and how to strip out specific characters.
Another option that covers any character other than a number can be seen in the following SO post, this is likely a much better solution to your problem.
Remove all but numbers from NSString
下面的代码只允许在输入的字符串中包含数字和
+
。The below code will allow only digits and
+
in the entered string.