开发者

How convert string in phone number format? objective c [closed]

开发者 https://www.devze.com 2023-04-05 12:23 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answ开发者_C百科ered in its current form. Fo
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answ开发者_C百科ered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消