思路:
取出字符串的每一个字符
判断字符是否是0-9 之间 计数器+1
@interface NSString (count)
-(void)countStrNum;
@end
@implementation NSString (count)
-(void)countStrNum{
int count=0;
//遍历字符串
for(int i=0;i<self.length;i++){
//获取字符串的每一个字符
unichar c = [self characterAtIndex:i];
//判断是否是阿拉伯数字
if(c>='0'&& c<='9') {
//如果是阿拉伯数字,则+1
count++;
}
}
NSLog(@"count = %d",count);
}
|
|