Search code examples
iosobjective-cstringnsarray

Separate a string into different Array


I have a string coming from server , i want to show some part of string in a view and rest part of string in other view .

Thanks for help.

i want to get the last word of last line of first view. i have searched ,but nothing seems to help me. If any one can suggest me something i would be glad to him/her.

 NSString *fromIndex = [_categoryWiseNews.article substringWithRange:NSMakeRange(0, lastWordOfFirstView)];  

i have tried with substringWithRange but with this you have to know the lastWordOfFirstView index number for breaking the string into two parts, for which i have no idea.


Solution

  • I really don't know what it is that you're asking lastWordOfFirstView might make sense to you because it is relevant to the thing you are working on. But to us it means nothing.

    You can separate a string into words in an array doing this...

    NSString *string = @"Hello world, this is a string";
    
    NSArray *array = [string componentsSeparatedByString:@" "];
    
    // array is now... [@"Hello", @"world,", @"this", @"is", @"a", @"string"];
    

    Then you can get the last word from it...

    NSString *lastWord = [array lastObject];