代碼:
- (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