简体   繁体   中英

How to split string in iphone programming

I have one NSMutableArray in 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.

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

我想你只需要修剪\\ n而不是拆分。

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM