views:

773

answers:

1

I need to split string without a separator. Here is the code..

myarray = [mystring componentsSeparatedByString:@","];

Here I used separator "," to split string, but I want to split without using a separator

NSString *mystring =@"123456";

I have this value , now I want to insert that value in an array like this:

array[0]=1;
array[1]=2;

............and so on..

+3  A: 

This sort of question is a bit tricky when you consider internationalization. For example, if you go the "dumb algorithm" route and just take the characters, any multibyte character will get messed up. This is probably the closest simple algorithm you'll get to handling different languages well:

- (NSArray *)arrayOfCharacters {
    int startIndex = 0;
    NSMutableArray *resultStrings = [NSMutableArray array];
    while (startIndex < [self length]) {
        NSRange characterRange = [self rangeOfComposedCharacterSequenceAtIndex:startIndex];
        [resultStrings addObject:[self substringWithRange:characterRange]];
        startIndex += characterRange.length;
    }
    return [[resultStrings copy] autorelease];
}

Even that's far from perfect, though — not all languages necessarily count characters the same way we do.

Chuck