Adam Carter
Adam Carter

Reputation: 4844

Counting occurrences of capital letters and numbers in an NSString

In PHP I am using the following code...

$passwordCapitalLettersLength = strlen(preg_replace("![^A-Z]+!", "", $password));
$passwordNumbersLength = strlen(preg_replace("/[0-9]/", "", $password));

...to count how many times capital letters and numbers appear in the password.

What is the equivalent of this in Objective C?

Upvotes: 4

Views: 1215

Answers (2)

WDUK
WDUK

Reputation: 19030

This can be done fairly concisely using the abilities of NSString and NSCharacterSet, instead of the need to iterate manually.

A decrement of 1 is required as componentsSeparatedByCharactersInSet: will always return at least one element, and that one element won't count your separations.

NSString* password = @"dhdjGHSJD7d56dhHDHa7d5bw3/%£hDJ7hdjs464525";

NSArray* capitalArr = [password componentsSeparatedByCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]];
NSLog(@"Number of capital letters: %ld", (unsigned long) capitalArr.count - 1);

NSArray* numericArr = [password componentsSeparatedByCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
NSLog(@"Number of numeric digits: %ld", (unsigned long) numericArr.count - 1);

Original answer: Whilst the code you've provided won't cover all bases, if you need to keep using those regular expressions for safety/risk reasons, you can do so below.

You can use RegEx in Objective-C. Saves manually iterating through the String, and keeps the code concise. It also means because you aren't iterating manually, you could potentially get a performance boost, as you can let the compiler/framework writer optimize it.

// Testing string
NSString* password = @"dhdjGHSJD7d56dhHDHa7d5bw3/%£hDJ7hdjs464525";

NSRegularExpression* capitalRegex = [NSRegularExpression regularExpressionWithPattern:@"[A-Z]"
                                                                              options:0
                                                                                error:nil];
NSRegularExpression* numbersRegex = [NSRegularExpression regularExpressionWithPattern:@"[0-9]"
                                                                              options:0
                                                                                error:nil];

NSLog(@"Number of capital letters: %ld", (unsigned long)[capitalRegex matchesInString:password
                                                                              options:0
                                                                                range:NSMakeRange(0, password.length)].count);
NSLog(@"Number of numeric digits: %ld", (unsigned long)[numbersRegex matchesInString:password
                                                                             options:0
                                                                               range:NSMakeRange(0, password.length)].count);

Upvotes: 1

Prine
Prine

Reputation: 12538

You can use the NSCharacterSet:

NSString *password = @"aas2dASDasd1asdASDasdas32D";
int occurrenceCapital = 0;  
int occurenceNumbers = 0;
for (int i = 0; i < [password length]; i++) {
    if([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[password characterAtIndex:i]])
       occurenceCapital++;

    if([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[password characterAtIndex:i]])
       occurenceNumbers++;

}

Upvotes: 7

Related Questions