Monika Patel Monika Patel - 1 year ago 71
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 Source
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]];
        string = [NSString stringWithFormat:@"%@ %@",string,stringArray[i]];


Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download