仅在前三个号码上被叫

时间:2016-12-08 06:42:06

标签: ios objective-c iphone

我的电话号码格式为(888)478-0041 。我正在创建一个链接,这意味着如果你点击该电话号码,它将自动拨打电话。但是在这种格式中,我的电话仅前三个数字是(888)。所以我该怎么做,我没有。

我有一个来自服务器的字符串:

<p>Your 2017 F-150 may need an oil change soon. &nbsp;Call&nbsp;</p><b><a href=\"tel:(888) 853-6045\">(888) 853-6045</a></b><p>&nbsp;or <a href=\"tel:(888) 478-0041\">(888) 478-0041</a> click</p><b>&nbsp;here</b><p>&nbsp;to schedule online. &nbsp;Jacob Williams, Service Manager</p>

我想在电话号码后删除任何空格,电话号码最多为10个字符。

我正在使用以下代码。

if ([serviceMessage containsString:@"tel:"]) {
     NSUInteger location = [serviceMessage rangeOfString:@"tel:"].location + 4;
     serviceMessage = [serviceMessage substringFromIndex:location];
     [serviceMessage substringWithRange:NSMakeRange(11,0)];
     serviceMessage =[serviceMessage substringToIndex:10];
     NSLog(@"New Trimmed string:%@",serviceMessage);
     serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:@" " withString:@""];
     NSLog(@"Final Trimmed string:%@",serviceMessage);
}

2 个答案:

答案 0 :(得分:1)

如果您只想替换特定范围内的空间并多次搜索tel:使用NSRegularExpression,则需要使用replaceOccurrencesOfString:withString:options:range:

NSString *serviceMessage = @"<p>Your 2017 F-150 may need an oil change soon. &nbsp;Call&nbsp;</p><b><a href=\"tel:(888) 853-6045\">(888) 853-6045</a></b><p>&nbsp;or <a href=\"tel:(888) 478-0041\">(888) 478-0041</a> click</p><b>&nbsp;here</b><p>&nbsp;to schedule online. &nbsp;Jacob Williams, Service Manager</p>";

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(tel:)" options:0 error:&error];
NSArray *matches = [regex matchesInString:serviceMessage options:0 range:NSMakeRange(0, serviceMessage.length)];
for (NSTextCheckingResult *match in matches) {
    NSRange wordRange = [match rangeAtIndex:1];
    NSUInteger location = wordRange.location + 4;
    serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:@" " withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(location, 13)];
}
NSLog(@"%@",serviceMessage);

<强>输出

<p>Your 2017 F-150 may need an oil change soon. &nbsp;Call&nbsp;</p><b><a href="tel:(888)853-6045">(888) 853-6045</a></b><p>&nbsp;or <a href="tel:(888)478-0041">(888) 478-0041</a> click</p><b>&nbsp;here</b><p>&nbsp;to schedule online. &nbsp;Jacob Williams, Service Manager</p>

答案 1 :(得分:0)

“Aditya Pandey先生”给出了一个非常好的技术解决方案。但如果你不熟悉iOS概念,如果你发现这个解决方案很难,你也可以选择以下选项:

if ([responceStr containsString:@"tel:"]) {
        NSUInteger location = [responceStr rangeOfString:@"tel:"].location;
        NSString *subStr = [responceStr substringFromIndex:location];
        NSString *endStr;
        if ([subStr containsString:@"\">"])
        {
            NSUInteger endLocation = [subStr rangeOfString:@"\">"].location;
            endStr = [subStr substringToIndex:endLocation];
        }
        NSLog(@"New Trimed string:%@",endStr);
        responceStr = [endStr stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSLog(@"Final Trimed string:%@",responceStr);
    }

您可以再次从 endStr 开始搜索下一个tel: