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

2

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?

Ephor answered 15/9, 2011 at 3:33 Comment(5)
What have you tried?Osteal
@Brian Roach Man brilliant link, I never seen that before..one to bookmark..Disseminule
Not a direct answer, but will help you..#665611Disseminule
I have the exact same problem. While we can code this ourself, there should be a standard code for this common problem. Not sure why the downvote.Delinda
This question is NOT ambiguous. It's not vague, incomplete or overly broad. Yes OP is lazy. But ambiguous he is not. C'mon guys.Delinda
P
3

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);
Psycho answered 15/9, 2011 at 4:7 Comment(0)
V
1

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

Valdemar answered 15/9, 2011 at 3:56 Comment(1)
Wow...i forget the "objective", its ok, convert it to objective c:P the logic is the same:PValdemar

© 2022 - 2024 — McMap. All rights reserved.