A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

#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;
}

3 个回复

倒序浏览
兄弟,你学的是C语言吗?好多年以前看的懂,现在都忘记了。
回复 使用道具 举报
oc加强!!!!!!点个赞
回复 使用道具 举报
貌似没啥问题,只是你重写NSString类,不知道能不能正常运行,得让我上机测试一下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马