iOS通讯录手机号格式化

  1. 去除数字以外的所有字符
NSCharacterSet *setToRemove = [[ NSCharacterSet characterSetWithCharactersInString:@"0123456789"]invertedSet];
NSString *strPhone = user.phones[0];
strPhone  = [[strPhone componentsSeparatedByCharactersInSet:setToRemove] componentsJoinedByString:@""];
  1. 去掉空格和换行符号的实质就是替换掉对应的字符
NSString* str=~~~~~~;
//1. 去除掉首尾的空白字符和换行字符
str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//2. 去除掉其它位置的空白字符和换行字符
str = [str stringByReplacingOccurrencesOfString:@"\r" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];

你可能感兴趣的:(iOS通讯录手机号格式化)