Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 909
Adam Carter Avatar asked Feb 19 '23 10:02

Adam Carter


1 Answers

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++;

}
like image 190
Prine Avatar answered Feb 21 '23 01:02

Prine