黑马程序员技术交流社区

标题: 统计字符串中的阿拉伯数字(代码) [打印本页]

作者: 过了任性的年纪    时间: 2016-3-16 22:15
标题: 统计字符串中的阿拉伯数字(代码)
#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;
}

作者: 陈文广    时间: 2016-3-16 22:30
兄弟,你学的是C语言吗?好多年以前看的懂,现在都忘记了。
作者: zhoujinhiu    时间: 2016-3-17 00:52
oc加强!!!!!!点个赞
作者: 为梦想再启程    时间: 2016-3-17 08:11
貌似没啥问题,只是你重写NSString类,不知道能不能正常运行,得让我上机测试一下




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2