在 NSString 中分隔数字以显示电话号码

发布于 2024-12-01 02:03:15 字数 164 浏览 0 评论 0原文

我有一个 NSString 包含这样的内容:

4434332124

我怎样才能把它变成这样的东西?

443-433-2124

I have an NSString that holds something like this:

4434332124

How can I make that into something like this?

443-433-2124

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

埖埖迣鎅 2024-12-08 02:03:15

只需使用 NSString 的 stringWithFormat 方法,将电话号码分割成单独的子字符串,然后将整个字符串粘合到格式字符串中,只需两行即可完成此操作。像这样的东西:

NSString *sPhone = @"4434332124";

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

编辑:工作代码

NSString *formatted = [NSString stringWithFormat: @"%@-%@-%@", [sPhone substringWithRange:NSMakeRange(0,3)],[sPhone substringWithRange:NSMakeRange(3,3)],
                           [sPhone substringWithRange:NSMakeRange(6,4)]];

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)]];
套路撩心 2024-12-08 02:03:15

如果您只想对您确定的 10 位数字进行简单的快速替换,请尝试此正则表达式示例 (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);

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);
帝王念 2024-12-08 02:03:15

不像尝试用 NSFormatter 做某事那么漂亮(尽管除非你将它子类化,否则我没有看到一个明显的方法让它做电话号码之类的事情),但你可以这样做:

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

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]];
醉城メ夜风 2024-12-08 02:03:15
#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;
}
#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文