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?
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" .
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.
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];
You can break the string into arrays of string and then manipulate as you want.
NSArray *brokenByLines=[yourString componentsSeparatedByString:@"\n"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With