复制电话号码消除多余字符

不知道是不是系统有问题,在通讯录复制的电话号码粘贴出来是15位,通过去掉字符串空格的办法处理之后,还是13位,明明看不到空格了,但长度还是13位。
下面是解决办法,希望能帮助到你:

//结束编辑
-(void)textViewDidEndEditing:(UITextView *)textView
{
// 去掉数字
NSLog(@"%lu",(unsigned long)textView.text.length);

NSMutableString *strippedString = [NSMutableString
stringWithCapacity:textView.text.length];
NSScanner *scanner = [NSScanner scannerWithString:textView.text];
NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:@"0123456789"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
[strippedString appendString:buffer];
}
else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
NSLog(@"%@", strippedString);
textView.text = strippedString;
NSLog(@"%lu",(unsigned long)textView.text.length);
}

你可能感兴趣的:(复制电话号码消除多余字符)