0

I have one NSMutableArrayin that array I am storing some strings which i got from web service response.in that array strings are:

"Test Test\n \n \n ", "abc Abc\n \n \n " "sss Pr\n \n \n ", "Gggf anil L\n \n \n ",

I want to split strings from \n.now how can i separate strings from \n. Please help me.Thanks in advance.

Sport
  • 8,570
  • 6
  • 46
  • 65
user3007459
  • 113
  • 1
  • 13
  • possible duplicate of [NSString tokenize in Objective-C](http://stackoverflow.com/questions/259956/nsstring-tokenize-in-objective-c) – Marco Nov 20 '13 at 06:11

4 Answers4

1
NSArray * components = [yourString componentsSeperatedByString:@"\n"];
for(NSString *str in components)
{
    NSLog(@"%@",str);
}
iGagan Kumar
  • 406
  • 8
  • 26
1

I think you just need to trim \n NOT the split.

NSString *trimmedValue = [array[0] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
0

In this case separating string from \n is BAD programming practice and the process is too much laindy. So better go for trimming string by \n. Try this -

NSString *trimmedString = [yourArray[0] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

Hope this will Help you. Thank you in advance.

iSmita
  • 1,292
  • 1
  • 9
  • 24
0

This action can be performed in 2 steps.

1 . NSArray * seperatedArr1 = [yourString componentsSeperatedByString:@","];

2. NSMutableArray *finalArr = [[NSMutableArray alloc]init] for(NSString *str in seperatedArr1) { NSString *trimmedString = [finalArr[0] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; [finalArr addobject:trimmedString]; }

megha
  • 84
  • 5