将 iPhone 电话号码格式转换为“可拨号”格式细绳

发布于 2025-01-03 05:39:07 字数 288 浏览 1 评论 0原文

我正在构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳男子 2025-01-10 05:39:07

这将删除所有非数字字符:

phoneNumDecimalsOnly = [[phoneNum componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

This will strip out all non-numeric characters:

phoneNumDecimalsOnly = [[phoneNum componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
简单 2025-01-10 05:39:07

要回答有关从字符串中删除特定字符的问题...请查看 NSString类参考具体方法stringByReplacingOccurrencesOfString:withString:

您可以这样做

fullNumer = [fullNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
fullNumer = [fullNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
fullNumer = [fullNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
fullNumer = [fullNumber 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

fullNumer = [fullNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
fullNumer = [fullNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
fullNumer = [fullNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
fullNumer = [fullNumber stringByReplacingOccurrencesOfString:@" " withString:@""];

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

溺ぐ爱和你が 2025-01-10 05:39:07

下面的代码只允许在输入的字符串中包含数字和 +

phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:
            [[NSCharacterSet characterSetWithCharactersInString:@"+0123456789"]
             invertedSet]]
           componentsJoinedByString:@""];

The below code will allow only digits and + in the entered string.

phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:
            [[NSCharacterSet characterSetWithCharactersInString:@"+0123456789"]
             invertedSet]]
           componentsJoinedByString:@""];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文