如何删除(从字符串

isc*_*ers 0 iphone ipad

可能重复:
iphone sdk - 从字符串中删除除数字0-9之外的所有字符

我有一个电话号码的字符串

mynumber = @"(334)687-6619";

我在用

NSString *phoneURLString = [NSString stringWithFormat:@"tel:%@", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];
Run Code Online (Sandbox Code Playgroud)

拨打电话.但由于"(",")"我的方法无法拨打电话.

请建议我做一个正确的字符串

"1-800-555-1212"或类似的东西.

谢谢

MS.*_*MS. 5

您可以使用

NSString *s = @"(334)687-6619";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"("];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];

s = [s stringByReplacingOccurrencesOfString:@")" withString:@"-"];
NSLog(@"%@", s);
Run Code Online (Sandbox Code Playgroud)

因此,它将取代"("与""和"替换")"与" - ".

谢谢.

  • 如果你分两步完成(这本身就是多余的),你甚至不必使用字符集和拆分连接技巧,而你只需要使用`stringByReplacingOccurrencesOfString:withString:`. (2认同)