Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I iterate through an NSString in objective c?

How can iterate through an NSString object in Objective c whiling maintaining an index for the character I am currently at?

I want to increment the ASCII value of every third character by 3, and then print this incremented character in a label in my user interface.

like image 688
Justin Copeland Avatar asked Dec 16 '22 02:12

Justin Copeland


1 Answers

Wasn't clear whether you just wanted to print the incremented characters or all. If the former, here's is how you would do it:

NSString *myString = @"myString";
NSMutableString *newString = [NSMutableString string];
for (int i = 0; i < [myString length]; i++) 
{
    int ascii = [myString characterAtIndex:i];
    if (i % 3 == 0) 
    {
        ascii++;
        [newString appendFormat:@"%c",ascii];
    }
}
myLabel.text = newString;
like image 168
Vinnie Avatar answered Dec 28 '22 08:12

Vinnie