字符串中含有数字和字母的个数

2016-06-27  本文已影响0人  东哥技术大菜逼

#define kCharacterAndNumber @"0123456789abcdefghijklmnopqrstuvwxyz/n"

// 字符串中含有数字和字母个数

- (NSInteger)countForNumberAndCharactersInString:(NSString *)string

{

NSInteger count = 0;

NSCharacterSet * charSet;

charSet = [[NSCharacterSet characterSetWithCharactersInString:kCharacterAndNumber] invertedSet];

for(int i = 0; i < [string length]; ++i) {

NSString * tempStr = [string substringWithRange:NSMakeRange(i, 1)];

NSString * filtered = [[tempStr componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];

if([tempStr isEqualToString:filtered]){

count++;

};

}

return count;

}

上一篇下一篇

猜你喜欢

热点阅读