代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"--iphone--%@",[self getNewPhoneStr1:@"18502368888"]); } //只需传一个电话进去,可以返回一个处理好的加了-线的400电话字符串,如传4001234567,返回400-123-4567 - (NSString *)getNewPhoneStr1:(NSString *)phonestr { if ([phonestr rangeOfString:@"-"].length == 0 && phonestr.length > 6) { NSMutableString *phoneNumber = [NSMutableString string]; NSString *subStr = [phonestr substringToIndex:3]; [phoneNumber appendFormat:@"%@-",subStr]; subStr = [phonestr substringWithRange:NSMakeRange(3, 3)]; [phoneNumber appendFormat:@"%@-",subStr]; subStr = [phonestr substringWithRange:NSMakeRange(6, phonestr.length-6)]; [phoneNumber appendString:subStr]; phonestr = phoneNumber; } return phonestr; }
输出:
2014-12-02 17:14:26.807 将400电话中间加上-线[21611:60b] --iphone--185-023-68888