Monika Patel Monika Patel - 5 months ago 9
Objective-C Question

How to calculate same substring from given entire NSString

I wan to calculate total white space in given string.If total white space is more then 1 then I want to replace 2nd number of space with "\n"

For example:
My string is : "I like iPhone" . This string contains 3 space.
So I want o/p like this "I like \niPhone".


This is just example. My string is not static. Give string is dynamic
How can I implement ??

Thanks in advance

Answer
NSString *givenString = @"I like iPhone ok";
NSArray *stringArray = [givenString componentsSeparatedByString:@" "];

NSString *string = @"";

for (int i = 0; i<stringArray.count; i++) {
    if (i==2) {
        string = [NSString stringWithFormat:@"%@\n%@",string,stringArray[i]];
    }else{
        string = [NSString stringWithFormat:@"%@ %@",string,stringArray[i]];
    }
}

NSLog(@"%@",string);

Hope this helps!