0

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?

4

2 回答 2

3

我猜这样的事情应该会解决;

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);
于 2011-09-15T04:07:20.300 回答
1

好的

假设你有旧手机

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

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

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

没有测试它。

希望有帮助

于 2011-09-15T03:56:11.360 回答