3

I have an NSString that holds something like this:

4434332124

How can I make that into something like this?

443-433-2124

jscs
  • 63,694
  • 13
  • 151
  • 195
James Dunay
  • 2,714
  • 8
  • 41
  • 66
  • 1
    Check out this previous post: http://stackoverflow.com/questions/665111/nsnumberformatter-to-format-us-telephone-numbers – Magic Bullet Dave Aug 23 '11 at 20:49
  • possible duplicate of [Phone Number Validation Formatting on iphone iOS](http://stackoverflow.com/questions/6052966/phone-number-validation-formatting-on-iphone-ios) – jscs Aug 23 '11 at 20:54

4 Answers4

12

This can be done in only two lines just use NSString's stringWithFormat method and chop up the phone number into individual substrings and glue the whole thing together in your format string. Something like this:

NSString *sPhone = @"4434332124";

NSString *formatted = [NSString stringWithFormat: @"%@-%@-%@", [sPhone substringWithRange:NSMakeRange(A,B)],[sPhone substringWithRange:NSMakeRange(B,C)],
                           [sPhone substringWithRange:NSMakeRange(C,D)]];

EDIT: Working Code

NSString *formatted = [NSString stringWithFormat: @"%@-%@-%@", [sPhone substringWithRange:NSMakeRange(0,3)],[sPhone substringWithRange:NSMakeRange(3,3)],
                           [sPhone substringWithRange:NSMakeRange(6,4)]];
Sam B
  • 27,273
  • 15
  • 84
  • 121
Nirma
  • 5,640
  • 4
  • 35
  • 47
  • how is this answer correct? what is A,B,C,D??? It doesn't even compile. Seriously man ... – Sam B Jul 13 '15 at 23:43
6

If you want to just do a simple quick replacement on what you are sure is a 10 digit number then try this regex example (iOS >= 3.2).

NSString *tenDigitNumber = @"5554449999";
tenDigitNumber = [tenDigitNumber stringByReplacingOccurrencesOfString:@"(\\d{3})(\\d{3})(\\d{4})"
                                                           withString:@"$1-$2-$3"
                                                              options:NSRegularExpressionSearch
                                                                range:NSMakeRange(0, [tenDigitNumber length])];
NSLog(@"%@", tenDigitNumber);
Joe
  • 56,979
  • 9
  • 128
  • 135
  • 1
    Any reason for the down vote? It is certainly less error prone than the accepted answers. – Joe Aug 23 '11 at 23:24
0

Not as pretty as trying to do something with NSFormatter (though unless you subclass it I didn't see an obvious way to get it to do something like phone numbers) but you could do this:

NSString* newString = [NSString stringWithFormat:@"%c%c%c-%c%c%c-%c%c%c%c" [phoneNumber characterAtIndex:0],
        [phoneNumber characterAtIndex:1],
        [phoneNumber characterAtIndex:2],
        [phoneNumber characterAtIndex:3],
        [phoneNumber characterAtIndex:4],
        [phoneNumber characterAtIndex:5],
        [phoneNumber characterAtIndex:6],
        [phoneNumber characterAtIndex:7],
        [phoneNumber characterAtIndex:8],
        [phoneNumber characterAtIndex:9]];
Clay
  • 319
  • 2
  • 15
0
#import <Foundation/Foundation.h>
static NSString * const theInString = @"4434332124";

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    const char      * theUniChar = theInString.UTF8String;
    NSMurableString * theResult = nil;


    for( int i = 0, c = (int)strlen(theUniChar); i < c; i+=2 )
    {
        char     thePart[4];
        sscanf( theUniChar + i, "%3s", &thePart );
        if( theResult == nil )
            theResult = [NSMutableString string];
        else
            [theResult appendFormate:@"-%3s",thePart];
    }

    printf( "%@\n", theResult );

    [pool drain];
    return 0;
}
Nathan Day
  • 5,981
  • 2
  • 24
  • 40