1.用正则表达式截取 字符串中的数字。
代码如下
1.过滤掉不是数字的字符串
//过滤掉不是数字的字符串
NSString *myPhoneNumber = @" 12 34678a9 ";
NSLog(@"%zd",myPhoneNumber.length); //输出:12
myPhoneNumber = [myPhoneNumber stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [myPhoneNumber length])];
NSLog(@"%zd",myPhoneNumber.length); //输出:8
NSLog(@"%@",myPhoneNumber); //输出:12346789
2. 去除空格
//1 去除字符串前后的空格
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
//2 去除字符串所有的空格
[string stringByReplacingOccurrencesOfString:@" " withString:@""]
3. 判断字符串是否为空
+ (BOOL) isEmpty: (NSString *)string {
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}
网友评论