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
You should be aware that \n is not the only character used to split a new line. For example, if the file was saved in Windows, the newline characters would be \r\n. Read the Newline article in Wikipedia for more information about this.
Thus, if you just use componentsSeparatedByString("\n"), you may get unexpected results.
let multiLineString = "Line 1\r\nLine 2\r\nLine 3\r\n"
let lineArray = multiLineStringRN.componentsSeparatedByString("\n")
// ["Line 1\r", "Line 2\r", "Line 3\r", ""]
Note both the residual \r and the empty array element.
There are several ways to avoid these problems.
1. componentsSeparatedByCharactersInSet
let multiLineString = "Line 1\nLine 2\r\nLine 3\n"
let newlineChars = NSCharacterSet.newlineCharacterSet()
let lineArray = multiLineString.componentsSeparatedByCharactersInSet(newlineChars).filter{!$0.isEmpty}
// "[Line 1, Line 2, Line 3]"
If filter were not used, then \r\n would produce an empty array element because it gets counted as two characters and so separates the string twice at the same location.
2. split
let multiLineString = "Line 1\nLine 2\r\nLine 3\n"
let newlineChars = NSCharacterSet.newlineCharacterSet()
let lineArray = multiLineString.utf16.split { newlineChars.characterIsMember($0) }.flatMap(String.init)
// "[Line 1, Line 2, Line 3]"
or
let multiLineString = "Line 1\nLine 2\r\nLine 3\n"
let lineArray = multiLineString.characters.split { $0 == "\n" || $0 == "\r\n" }.map(String.init)
// "[Line 1, Line 2, Line 3]"
Here \r\n gets counted as a single Swift character (an extended grapheme cluster)
3. enumerateLines
let multiLineString = "Line 1\nLine 2\r\nLine 3\n"
var lineArray = [String]()
multiLineString.enumerateLines { (line, stop) -> () in
lineArray.append(line)
}
// "[Line 1, Line 2, Line 3]"
For more about the enumerateLine syntax, see this answer also.
\r\n and \n but I am doing this here to show that these methods can handle both formats.\r and \n.