❮ zur Übersicht

Split NSString by characters

  • Autor:
  • Datum: 11.07.2010

Have you ever pondered on the NSString class reference and you were overwhelmed by the sheer amount of methods? I certainly have and due to the endless possibilities those methods give you, I just couldn’t figure out how to split an NSString by characters and create a NSArray of those characters.

Blame it on my lack of creativity or on the fact that I’m not a C buff. However, thanks to my creative Googling, I came across this site, which shows you how to do it:

NSMutableArray *characters = 
[[NSMutableArray alloc] initWithCapacity:[myString length]];
for (int i=0; i < [myString length]; i++) {
    NSString *ichar  = 
[NSString stringWithFormat:@"%c", [myString characterAtIndex:i]];
    [characters addObject:ichar];
}

Although Apple’s documentation is extensive, you sometimes need a little hint or guidance to achieve your goal.

Tags: objective-c