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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With