Sunday, October 11, 2009

iPhone dev Stupidity 49: Reverse a string

If you don't want to rewrite one, check this:


-(NSString *) reverseString
{
NSMutableString *reversedStr;
int len = [self length];
 
// Auto released string
reversedStr
= [NSMutableString stringWithCapacity:len];
 
// Probably woefully inefficient...
while (len > 0)
[reversedStr appendString:
[NSString stringWithFormat:@"%C", [self characterAtIndex:--len]]];
 
return reversedStr;
}

No comments: