Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to find out the best way to count the number of uppercase characters that are in a NSString. I know how to find out if a certain character is uppercase by using this code:

NSString *s = @"This is a string";
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]];

What would be the best way to count the number of uppercase letters in a NSString? Thanks.

like image 846
0SX Avatar asked Dec 27 '22 15:12

0SX


1 Answers

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.

like image 143
Dair Avatar answered Jan 28 '23 14:01

Dair