How to count the number of uppercase characters in a NSString?

3 Solutions Collect From Internet About “How to count the number of uppercase characters in a NSString?”

NSString *s = @"This is a string";  
int count=0;  
for (i = 0; i < [s length]; i++) {
    BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:i]];
    if (isUppercase == YES)
       count++;
}

count is the number of uppercase occurences.

Walk through the indices in the string one by one and add 1 to a counter whenever you find an uppercase letter.

One line solution

 NSUInteger count = [[[@"A string HERE!!" componentsSeparatedByCharactersInSet:[[NSCharacterSet uppercaseLetterCharacterSet] invertedSet]] componentsJoinedByString:@""] length]; // count = 5