遍历字符串 返回字符重复出现的次数

遍历字符串 返回字符重复出现的次数

 

 NSString *string = [NSString stringWithFormat:@"liugang"];

 char g = 'g';



+(NSInteger)CharInNSString:(NSString *)string Char:(char)c

{

    NSInteger x = 0;

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

        if ((char)[string characterAtIndex:i] == c ) {

            NSLog(@"%c",(char)[string characterAtIndex:i]);

            x++;

        }

    }

    return x;

}

字符串方法characterAtIndex 可以获取字符串下标对应字符 返回类型为unichar

[string length]返回字符串长度

(char)[string characterAtIndex:i] 返回字符串下标对应字符输出格式化为char 用%C来接

你可能感兴趣的:(自修,字符串宽高)