gpt4 book ai didi

ios - 仅调用前三个号码

转载 作者:行者123 更新时间:2023-11-29 00:34:09 32 4
gpt4 key购买 nike

我收到格式为 (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);
}

最佳答案

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

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>

关于ios - 仅调用前三个号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41033330/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com