user3007459
user3007459

Reputation: 113

How to split string in iphone programming

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.

Upvotes: 0

Views: 136

Answers (4)

megha
megha

Reputation: 84

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]; }

Upvotes: 0

iSmita
iSmita

Reputation: 1292

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.

Upvotes: 0

βhargavḯ
βhargavḯ

Reputation: 9836

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

NSString *trimmedValue = [array[0] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

Upvotes: 1

iGagan Kumar
iGagan Kumar

Reputation: 406

NSArray * components = [yourString componentsSeperatedByString:@"\n"];
for(NSString *str in components)
{
    NSLog(@"%@",str);
}

Upvotes: 1

Related Questions