Daniel Tkach7,607 Points
No \r, only \n
Hello folks, I only have a \n, and well I can use the Split method with '\n\' as a parameter and it works just fine. I was wondering why I don't have this \r and if there are any implications. I guess before splitting a csv file I should add some code to check if the \r is present? What do you think?
Steven Parker203,115 Points
It wouldn't hurt to play it safe.
Depending on the OS, the standard for line ending might be just "
\n" (Linux or Mac), or "
Just to be safe, you might want to do a
Replace("\r\n", "\n") before the split.
Ole Vølund Skov MortensenCourses Plus Student 23,115 Points
OK, I got Your answers. If You get the above issue, select the csv file. click text, ctrl+A, and look for the LF in the lower right. click and select CRLF(Caridge return, line feed). and save. run the program again and you have it fixed.
For the purpose of splitting the csv file into an array of substrings i do agree that having one character to use for delimiting makes it easier and hence replacing \r\n with just \n would serve perfect for the task, all i am saying is that for the purpose of those who would wish to follow closely along with the video, the csv file provided seems to only have \n as the newline character, and hence using \n as the only delimiting character does not bring about the problem of empty strings in the resulting string array and so if people would so wish to see this problem first hand they would rather replace the \n with \r\n to get a clear understanding of why we are using the StringSplitOptions.RemoveEmptyEntries at the end to solve this problem.
Also, if in a case of generally creating text files to be read across many windows systems, \r\n would be better as a go to newline character to be safe, as it is backwards compatible with older windows systems that only recognize the combination of both characters as a new line character.
i believe having both "\r\n" is a good thing for backwards compatibility with older windows systems that require both the carriage return and line feed characters, in the case of a text file only having "\n", calling the Replace("\n" , "\r\n") would replace all instances of the escape sequence "\n" in the text file with "\r\n" thereby making it backwards compatible with older systems. but if it's only for the cause of the tutorial then only "\n" would do just fine.