I have a large string set something like - Objective-C is the primary programming language you use when writing software for OS X and iOS. Itβs a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime. Objective-C inherits the syntax, primitive types, and flow control statements of C and adds syntax for defining classes and methods. It also adds language-level support for object graph management and object literals while providing dynamic typing and binding, deferring many responsibilities until runtime.
I wants to divide above string in the length of at least 15 characters strings which will be ending by whitespaces & store in an array. For this I have applied following logic which is working fine , But it will be busy the compiler in case of long strings like 1000000 characters , Please help me if you can suggest better way to meet by requirements .Thanks.
NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
NSString *shortDescription = shortDesc_;
NSString *str=@"";
NSArray *iarray=[shortDescription componentsSeparatedByString:@" "];
NSInteger i=0;
while (i<iarray.count)
{
NSString *prevStr=str;
str=[prevStr stringByAppendingString:[[iarray objectAtIndex:i] stringByAppendingString:@" "]];
if(str.length>15)
{
[randomSelection addObject:prevStr];
str=[[iarray objectAtIndex:i] stringByAppendingString:@" "];
}
i++;
}
if(str.length>0)
[randomSelection addObject:str];