#import <Foundation/Foundation.h>
@interface NSString (Count)
- (void)countNumForString;
@end
@implementation NSString (Count)
- (void)countNumForString{
int count = 0;
for (int i = 0; i<self.length; i++) {
//获取每个字符
unichar ch = [self characterAtIndex:i];
//判断字符是否是数字
if (ch>='0' && ch<='9') {
count ++;
}
}
NSLog(@"当前字符串%@中含有数字的个数是:%d",self,count);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"a15d1a51aefaef5e1f5a1";
[str countNumForString];
}
return 0;
}
|
|