如何将字符串转换为电话号码格式?目标c

发布于 2024-12-04 13:10:28 字数 107 浏览 11 评论 0原文

我想将字符串转换为电话号码格式。例如,我有一个字符串,其中包含值“456897012”。现在我想将其转换为电话号码格式,例如 (456)897-012。 那么这个过程是怎样的呢?我怎样才能做到这一点?

I want to convert string into phone number format. For example I have string in which I have value, "456897012". Now I want to convert it in phone number format like as (456)897-012.
So what is process for that? How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

十秒萌定你 2024-12-11 13:10:28

我想这样的事情应该能解决;

NSString *unformatted = @"5129876985";
NSArray *stringComponents = [NSArray arrayWithObjects:[unformatted substringWithRange:NSMakeRange(0, 3)], 
                             [unformatted substringWithRange:NSMakeRange(3, 3)], 
                             [unformatted substringWithRange:NSMakeRange(6, [unformatted length]-6)], nil];

NSString *formattedString = [NSString stringWithFormat:@"(%@)%@-%@", [stringComponents objectAtIndex:0], [stringComponents objectAtIndex:1], [stringComponents objectAtIndex:2]];
NSLog(@"Formatted Phone Number: %@", formattedString);

Something like this should work out I guess;

NSString *unformatted = @"5129876985";
NSArray *stringComponents = [NSArray arrayWithObjects:[unformatted substringWithRange:NSMakeRange(0, 3)], 
                             [unformatted substringWithRange:NSMakeRange(3, 3)], 
                             [unformatted substringWithRange:NSMakeRange(6, [unformatted length]-6)], nil];

NSString *formattedString = [NSString stringWithFormat:@"(%@)%@-%@", [stringComponents objectAtIndex:0], [stringComponents objectAtIndex:1], [stringComponents objectAtIndex:2]];
NSLog(@"Formatted Phone Number: %@", formattedString);
于我来说 2024-12-11 13:10:28

好吧,

假设你有旧手机

[oldPhone insertString: @"(" atIndex: 0];

[oldPhone insertString: @")" atIndex: 4];

[oldPhone insertString: @"-" atIndex: 9];

还没有测试过。

希望有帮助

Ok

Suppose you have the oldPhone

[oldPhone insertString: @"(" atIndex: 0];

[oldPhone insertString: @")" atIndex: 4];

[oldPhone insertString: @"-" atIndex: 9];

Have not test it.

Hope that helps

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