我希望将放置在NSTextField / UITextField中的任何文本格式化为
###-###-####
以下是一些可能的输入
1112223333
111.222.3333
(111)222-3333
(111) 2223333
答案 0 :(得分:3)
我已经测试了这段代码,它适用于您的所有示例:
- (NSString *)convertNumber:(NSString *)input {
NSMutableString *result = [NSMutableString stringWithCapacity:15];
NSScanner *scanner = [NSScanner scannerWithString:input];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
NSString *temp;
NSInteger location = 0;
// Get first group
[scanner scanUpToCharactersFromSet:numbers intoString:&temp];
location = [scanner scanLocation];
[scanner scanCharactersFromSet:numbers intoString:&temp];
[result appendString:temp];
if ([scanner scanLocation] < location + 4) {
// Only scanned three numbers - get second group
[scanner scanUpToCharactersFromSet:numbers intoString:&temp];
location = [scanner scanLocation];
[scanner scanCharactersFromSet:numbers intoString:&temp];
[result appendString:temp];
if ([scanner scanLocation] < location + 4) {
// Only scanned three numbers - get last group
[scanner scanUpToCharactersFromSet:numbers intoString:&temp];
[scanner scanCharactersFromSet:numbers intoString:&temp];
[result appendString:temp];
}
} else if ([scanner scanLocation] < location + 7) {
// Scanned six numbers - get last group
[scanner scanUpToCharactersFromSet:numbers intoString:&temp];
[scanner scanCharactersFromSet:numbers intoString:&temp];
[result appendString:temp];
}
// Add dashes
if ([result length] > 6) {
[result insertString:@"-" atIndex:3];
[result insertString:@"-" atIndex:7];
}
return result;
}
将忽略3组,3组和4组之间任何数量的非数字字符(或无)。
警告:它可能会在整个地方泄漏,如果给出意外输入可能会崩溃。
答案 1 :(得分:1)
正则表达式是你的朋友:
NSString * reformat( NSString * number )
{
NSRegularExpression * noDigits =
[ NSRegularExpression regularExpressionWithPattern: @"\\D" options: 0 error: NULL ] ;
NSString * onlyDigits = [ noDigits stringByReplacingMatchesInString: number
options: 0
range: NSMakeRange ( 0, number.length )
withTemplate: @"" ] ;
NSRegularExpression * phoneNumberPattern =
[ NSRegularExpression regularExpressionWithPattern: @"^(\\d\\d\\d)(\\d\\d\\d)(\\d\\d\\d\\d)$"
options: 0
error: NULL ] ;
NSString * result = [ phoneNumberPattern stringByReplacingMatchesInString: onlyDigits
options: 0
range: NSMakeRange( 0, onlyDigits.length )
withTemplate: @"$1-$2-$3" ] ;
return result ;
}
如果遇到10位以外的其他内容,您可能需要查看最后一次模式匹配替换的替代方法。或者,在计算onlyDigits
之后立即进行长度检查。
答案 2 :(得分:0)
试试这个方法
-(void)NumberFormatter:(NSString *)originalString
{
NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ"];
if ([originalString rangeOfCharacterFromSet:set].location == NSNotFound)
{
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:originalString.length];
NSScanner *scanner = [NSScanner scannerWithString:originalString];
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)];
}
}
if ([strippedString length]<10) {
NSLog(@"it is invalid string ,must contain atleast 10 numbers");
}
else
{
NSString *finalstring= [NSString stringWithFormat:@"%@-%@-%@",[strippedString substringWithRange:NSMakeRange(0, 3)],[strippedString substringWithRange:NSMakeRange(3, 3)],[strippedString substringFromIndex:6]];
NSLog(@"This is the formatted number==%@",finalstring);
}
}
else
NSLog(@"string is invalid,It contains characters");
}