views:

601

answers:

1
    NSString *origString = @"1 888 2313 1231"; // or anything like that, 423-2342 should work too
[[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] length] == 7 ? 
([NSString stringWithFormat:@"%@ (%@) %@-%@", 
    [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] length] == 10 ? 
    [NSArray arrayWithObjects:
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(0, 1)],
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(1, 3)],
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(4, 6)],
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(7, 10)]
     ] : 
    [NSArray arrayWithObjects:
        @"1",
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(0, 2)],
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(4, 5)],
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(6, 9)]
     ]
]) :
([NSString stringWithFormat:@"%@-%@", 
    [NSArray arrayWithObjects:
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(0, 1)],
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(1, 3)],
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(4, 6)],
        [[[origString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""] substringWithRange:NSMakeRange(7, 10)]
    ]
 ]);
A: 

See http://stackoverflow.com/questions/1382973/convert-nsstring-with-a-number-to-appropriate-phone-number-localization-format (and file an enhancement request about this, if you want it).

http://www.iphonesdkarticles.com/2008/11/localizating-iphone-apps-custom.html also describes creating a formatter to do what you want.

Mike
Thanks. My code wasn't nearly so nice ;P.
chpwn