How do I split an NSString by each character in the string?

Posted by Robert Höglund on Stack Overflow See other posts from Stack Overflow or by Robert Höglund
Published on 2010-03-18T10:29:17Z Indexed on 2010/03/18 10:31 UTC
Read the original article Hit count: 324

Filed under:
|

I have the following code, which works as I expect. What I would like to know if there is an accepted Cocoa way of splitting a string into an array with each character of the string as an object in an array?

- (NSString *)doStuffWithString:(NSString *)string {
    NSMutableArray *stringBuffer = [NSMutableArray arrayWithCapacity:[string length]];
    for (int i = 0; i < [string length]; i++) {
        [stringBuffer addObject:[NSString stringWithFormat:@"%C", [string characterAtIndex:i]]];
    }

    // doing stuff with the array

    return [stringBuffer componentsJoinedByString:@""];
}

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about objective-c