zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    去除字符串中的重复字符
    .net生成的类,跨工程调用显示注释
    Flex 页面空白或Error #2032: 流错误处理办法
    读取点阵字库
    SQL多表联合查询(Access数据库表)
    MSComm不能触发MSComm1_OnComm()事件原因之一
    一个小时内学习SQLite数据库(转)
    人生无悔
    学习之道
    挺经
  • 原文地址:https://www.cnblogs.com/yaowen/p/7436231.html
Copyright © 2011-2022 走看看