How can I split a string without a delimiter?
I need to split a string without a delimiter. Here is the code.
myarray = [mystring componentsSeparatedByString:@","];
Here I used the separator "," to split the string, but I want to split without using the separator
NSString *mystring =@"123456";
I have this value, now I want to insert this value into an array like this:
array[0]=1;
array[1]=2;
............ etc.
+2
Chris
source
to share
1 answer
A question like this is a bit tricky if you are considering internationalization. For example, if you go the "dumb algorithm" route and just pick up characters, any multibyte character will get confused. This is probably the closest simple algorithm that you can handle well in different languages:
- (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 this is far from ideal, but not all languages โโnecessarily take into account characters in the same way that we do.
+3
source to share