NSMutableArray* array1 = [[NSMutableArray alloc] init];
int divide_factor = <your divide_factor>;
while (yourString.length) {
NSString* substring = [yourString substringWithRange:NSMakeRange(0, MIN(divide_factor, yourString.length))];
[array1 addObject:substring];
yourString = [yourString stringByReplacingCharactersInRange:NSMakeRange(0, MIN(divide_factor, yourString.length)) withString:@""];
}
The advantage is that, irrespective of the length of string, it does not throw any exceptions. The last substring will be the leftover substring after division.
TEST CASES:
(1)
yourString = @"abcdefghijklmnopqrstuvwxyz";
divide_factor = 3;
Then results is:
(
abc,
def,
ghi,
jkl,
mno,
pqr,
stu,
vwx,
yz
)
(2)
yourString = @"abcdefghijklmnopqrstuvwxyz";
divide_factor = 7;
result:
(
abcdefg,
hijklmn,
opqrstu,
vwxyz
)