oc60--Category 分类 练习
2017-08-26 19:51
120 查看
// 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
相关文章推荐
- 08-给NSString增加类方法(分类练习01)
- #小练习 SGMLParser练习 分类: HTMLParser 2013-11-12 15:50 456人阅读 评论(0) 收藏
- OC--category(类目、分类、类别)
- 分类(Category)使用
- 分类(category)、协议(protocol)
- iOS 使用分类category和扩展extension
- #小练习 合并首字母相同的男孩、女孩姓名 (最终版) 分类: python 小练习 2013-11-25 16:52 283人阅读 评论(0) 收藏
- tensoflow练习3:卷积神经网络用于分类
- 猜驴 分类: python 小练习 2013-06-28 10:37 216人阅读 评论(0) 收藏
- 递归调用输出元素 分类: python 小练习 python基础学习 2013-06-30 11:54 278人阅读 评论(0) 收藏
- 去除文件中的重复元素后写入文件 分类: python 小练习 open()文件读写 2013-12-03 11:21 356人阅读 评论(0) 收藏
- 人民币金额打印 分类: python 小练习 python基础学习 2013-12-06 12:38 336人阅读 评论(0) 收藏
- c++笔记03---构造函数,初始化表,文件分类,钟表练习,析构函数,this 指针
- OpenCart之商品分类(Category)模块教程
- 图结构练习——最小生成树 分类: 最小生成树 图论 2015-06-09 17:00 13人阅读 评论(0) 收藏
- magento -- 如何为商品分类(category)添加自定义属性
- fileinput模块 分类: python基础学习 python 小练习 python Module 2013-08-15 17:18 417人阅读 评论(0) 收藏
- Objective-C /iphone开发基础:分类(category,又称类别)
- 先执行linux的clear清屏命令,再执行其他操作 分类: python 小练习 2013-09-13 11:23 441人阅读 评论(0) 收藏
- matlab练习程序(神经网络分类)