Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a for loop in this argument

    for (int i=0; i<[rawNumber length]; i++) {
        NSString* chr = [rawNumber substringWithRange:NSMakeRange(i, 1)];
        if(doesStringContain(@"0123456789", chr)) {
            telNumber = [telNumber stringByAppendingFormat:@"%@", chr];
        }
    }

What's the logic of this? What does this argument return?

like image 219
Micheal A Avatar asked Sep 15 '26 08:09

Micheal A


1 Answers

Looks like it's stripping out all the non-numeric characters, to give you a plain old phone number.

I imagine telNumber is defined before this loop, and uses the value of telNumber somewhere else.

Lets say rawNumber held the following value: (987)-654-3210. The for loop runs 14 times total, since that's the length of rawNumber. Each time through, the code gets a single character - the first time it gets the first character, the second time the second character, etc. Each time through the loop, the code checks if the character is in the string 0123456789; if it is, then the code appends the character to the telNumber variable. If the character is not in the list of numbers (if it's ( or ) or - in our example), then it's just discarded.

like image 153
Thorin Avatar answered Sep 16 '26 20:09

Thorin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!