category应用(计算nssting的数量)

时间:2023-03-09 02:51:40
category应用(计算nssting的数量)

//

//  main.m

//  03-分类应用

//

//  Created by apple on 14-3-18.

//  Copyright (c) 2014年 apple. All rights reserved.

//

#import <Foundation/Foundation.h>

/*

统计一个字符串中,出现的整数的个数

*/

#import "NSString+NSStringExtend.h"

int main(int argc, const char * argv[])

{

@autoreleasepool {

//        NSString * str = @"ab1d3,5,5,4";

//

//        int count = [NSString numberCountOfString:str];

//        NSLog(@"%d",count);

//        NSString * str = @"adf378";//字符串对象

//

//

//        NSLog(@"new %d",[@"adf378" numberOfCount]);

int count = [NSString numberCountOfString:@"12345"];

NSLog(@"%d",count);

}

return 0;

}

//

//  NSString+NSStringExtend.m

//  02-OC中特有的一些语法

//

//  Created by apple on 14-3-18.

//  Copyright (c) 2014年 apple. All rights reserved.

//

#import "NSString+NSStringExtend.h"

@implementation NSString (NSStringExtend)

//ab1c890

+ (int)numberCountOfString:(NSString *)str

{

/*

int count = 0;

//1.把每一个字符拿出来比较

for (int i = 0; i < str.length; i++)

{

unichar c = [str characterAtIndex:i];

if (c >= '0' && c <= '9' )

{

count++;

}

}

return count;

*/

return [str numberOfCount];

}

- (int)numberOfCount

{

int count = 0;

//1.把每一个字符拿出来比较

for (int i = 0; i < self.length; i++)

{

unichar c = [self characterAtIndex:i];

if (c >= '0' && c <= '9' )

{

count++;

}

}

return count;

}

@end