Iphone 如何从字符串中删除
可能重复:Iphone 如何从字符串中删除,iphone,ipad,Iphone,Ipad,可能重复: 我有一个电话号码的字符串 我的号码=@“(334)687-6619” 我正在使用 NSString *phoneURLString = [NSString stringWithFormat:@"tel:%@", phoneNumber]; NSURL *phoneURL = [NSURL URLWithString:phoneURLString]; [[UIApplication sharedApplication] openURL:phoneURL]; 打电话。 但是由于“(“
我有一个电话号码的字符串 我的号码=@“(334)687-6619” 我正在使用
NSString *phoneURLString = [NSString stringWithFormat:@"tel:%@", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];
打电话。
但是由于“(“,”)的原因,我的方法无法进行调用
请建议我做一根合适的绳子
差不多
“1-800-555-1212”之类的
感谢您从字符串中删除字符:
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"()"];
NSArray *comps = [string componentsSeparatedByCharactersInSet:set];
NSString *newString = [comps componentsJoinedByString:@""];
你可以用
NSString *s = @"(334)687-6619";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"("];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
s = [s stringByReplacingOccurrencesOfString:@")" withString:@"-"];
NSLog(@"%@", s);
因此,它将以“-”取代“(“替换为“&replace”)”
谢谢。使用此代码
NSString *str = @"(334)687-6619";
str = [str stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];
str = [str stringByReplacingCharactersInRange:NSMakeRange(3, 1) withString:@"-"];
NSLog(@"%@",str);
输出为:334-687-6619如果分两步执行(这本身是多余的),您甚至不必使用字符集和拆分并连接技巧,而只需使用
stringByReplacingOccurrencesOfString:withString:
。好了,我的朋友,它起作用了。问题是关于替换字符串。所以我把.s=[s stringByReplacingOccurrencesOfString:@]“withString:@”];他也在工作。谢谢你,伙计:)我认为@iManan是对的。会的work@CarlVeazey你确定这是重复的吗。。另一个线程也从字符串中删除了“-”。我得到了我所期望的结果。哦@Carl Veazey你说得对..回答得不错@joshi。。。。