使用 NSScanner 提取 NSString

发布于 2024-10-30 17:31:58 字数 98 浏览 2 评论 0原文

我正在从 AllContacts 获取数据;在该数据中,我获得了联系方式,例如 (998) 989-8989。使用这个号码,我无法拨打电话。任何人都可以帮忙解决这个问题吗?提前致谢。

I'm fetching data from AllContacts; in that data I'm getting contact details such as (998) 989-8989. Using this number, I'm not able to make a call. Can any one help out with this? Thanks in advance.

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

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

发布评论

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

评论(2

予囚 2024-11-06 17:32:04

大家好

最后我使用了以下代码来解决这个问题

NSString *originalString = @"(998) 989-8989";
NSMutableString *strippedString = [NSMutableString 
                                   stringWithCapacity:originalString.length];

NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet 
                           characterSetWithCharactersInString:@"0123456789"];

while ([scanner isAtEnd] == NO) {
    NSString *buffer;
    if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
        [strippedString appendString:buffer];

    } else {
        [scanner setScanLocation:([scanner scanLocation] + 1)];
    }
}

NSLog(@"%@", strippedString);

谢谢大家

HI All

At last i have used this following code to resolve this issue

NSString *originalString = @"(998) 989-8989";
NSMutableString *strippedString = [NSMutableString 
                                   stringWithCapacity:originalString.length];

NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet 
                           characterSetWithCharactersInString:@"0123456789"];

while ([scanner isAtEnd] == NO) {
    NSString *buffer;
    if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
        [strippedString appendString:buffer];

    } else {
        [scanner setScanLocation:([scanner scanLocation] + 1)];
    }
}

NSLog(@"%@", strippedString);

Thanks All

时光是把杀猪刀 2024-11-06 17:32:04

听起来你可以删除空格、括号和连字符。

NSString *phoneNumber = @"(998) 989-8989";
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phoneNumber = [@"tel://" stringByAppendingString:phoneNumber];
[[UIApplication sharedApplication] openURL:phoneNumber];

Sounds like you can just remove spaces, brackets and hypens.

NSString *phoneNumber = @"(998) 989-8989";
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phoneNumber = [@"tel://" stringByAppendingString:phoneNumber];
[[UIApplication sharedApplication] openURL:phoneNumber];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文