• oc60--Category 分类 练习


    //  main.m
    //  Category练习
    
    #import <Foundation/Foundation.h>
    #import "NSString+NJ.h"    //看不到NSString的.h文件。
    
    /*
    int countWithStr(NSString *str)
    {
        int count = 0;
        for (int i = 0; i < str.length; ++i) {
            unichar c = [str characterAtIndex:i];
            if (c >= '0' && c <= '9') {
                count++;
            }
        }
        return count;
    }
     */
    
    int main(int argc, const char * argv[]) {
        /*
         已知一个字符串, 要求找出字符串中所有的阿拉伯数字
         @"a123jj46kfd5jlwf7ld";
         
         1.计数器思想, 定义一个变量保存结果
         2.遍历字符串, 取出字符串中所有的字符
         */
        
        NSString *str = @"a1jj46kf1d5jlwf7l9d8";
        /*
    //    unichar c = [str characterAtIndex:1];
    //    NSLog(@"%c", c);
        int count = 0;
        for (int i = 0; i < str.length; ++i) {
            unichar c = [str characterAtIndex:i];
    //        NSLog(@"%c", c);
            if (c >= '0' && c <= '9') {
                count++;
            }
        }
         */
        
        int count2 = countWithStr(str);
        int count1 = [NSString countWithStr:str];
        int count = [str count];
        NSLog(@"count = %i", count);
        return 0;
    }
    //  NSString+NJ.h
    
    #import <Foundation/Foundation.h>
    
    @interface NSString (NJ)
    
    + (int)countWithStr:(NSString *)str;
    
    - (int)count;
    @end
    //  NSString+NJ.m
    
    #import "NSString+NJ.h"
    
    @implementation NSString (NJ)
    
    
    
    -(int)countWithStr:(NSString *)str{
        int count=0;
        for (int i=0; i< str.length; i++) {
            unichar c=[str characterAtIndex:i];
            if (c>='0'&& c<='9') {
                count++;
            }
        }
    
    
    }
    
    
    
    -(int)count{
        int number=0;
        for (int i= 0; i< self.length; ++i) {
            unichar c=[self characterAtIndex:i];
            if(c>='0'&& c<='0');
            number ++;
                
        }
    
    
    }
    @end
    //  Person.h
    
    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject
    
    - (void)test;
    @end
    //  Person.m
    
    #import "Person.h"
    #import "NSString+NJ.h"
    
    @implementation Person
    
    
    -(void)test{
       NSString *str=@"fds64jkl43fjdslkf";
        int count =[NSString countWithStr:str];
        NSLog(@" count= %i",count);
    }
    
    
    
    @end
  • 相关阅读:
    django项目环境搭建备忘
    Python IDE的选择和安装
    MAC上python环境搭建
    hadoop1.2.1+hbase0.90.4+nutch2.2.1+elasticsearch0.90.5配置(伪分布式)
    ubuntu下hadoop完全分布式部署
    ubuntu下集群设置静态ip
    C语言调用库函数实现生产者消费者问题
    poj 1703(带权并查集)
    poj 1330
    poj1724
  • 原文地址:https://www.cnblogs.com/yaowen/p/7436231.html
Copyright © 2020-2023  润新知