Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split an NSString by character count?

What is the simplest way to split an NSString by the maximum number of characters?

For example, if I have a string that is 204 characters long and I want to split it at 100 characters, it should yield an NSArray with three substrings (100,100,4).

like image 808
Kristina Brooks Avatar asked Jan 19 '23 10:01

Kristina Brooks


1 Answers

NSMutableArray *array = [[NSMutableArray alloc] init];
int len=0;

while( (len+100)<[string length]) {
    [array addObject:[string substringWithRange:NSMakeRange(len,100)]];
    len+=100;
}

[array addObject:[string substringFromIndex:len]];

Don't forget to release the array when you have done.

like image 72
Manlio Avatar answered Feb 01 '23 10:02

Manlio