Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string with newlines

I read from a csv file, and want to split the long string that I get using stringWithContentsOfFile, which is a multi line string, with individual lines representing rows in the csv file. How do I do this?

like image 585
Sonu Jha Avatar asked Mar 01 '13 11:03

Sonu Jha


People also ask

How do you split a string into a new line in Python?

To split a string by newline character in Python, pass the newline character "\n" as a delimiter to the split() function. It returns a list of strings resulting from splitting the original string on the occurrences of a newline, "\n" .

How do you separate a new line in Java?

2.2.The “\n” character separates lines in Unix, Linux, and macOS. On the other hand, the “\r\n” character separates lines in Windows Environment. Finally, the “\r” character separates lines in Mac OS 9 and earlier.


2 Answers

Just in case anyone stumbles across this question like I did. This will work with any newline characters:

NSCharacterSet *separator = [NSCharacterSet newlineCharacterSet]; NSArray *rows = [yourString componentsSeparatedByCharactersInSet:separator]; 
like image 118
Diana Farin Avatar answered Oct 08 '22 15:10

Diana Farin


You can break the string into arrays of string and then manipulate as you want.

NSArray *brokenByLines=[yourString componentsSeparatedByString:@"\n"] 
like image 23
Dipendra Avatar answered Oct 08 '22 15:10

Dipendra