|
-
Nov 11th, 2012, 02:22 PM
#1
Thread Starter
Fanatic Member
What is a line in a textfile?
Watching this post in another thread, I suddenly remembered once I tried something similar and got some empty lines. This prompted me to try an experiment:
Are the following equivalent:
* File.ReadAllLines(filename)
* File.ReadAllText(filename).Split(Environment.NewLine) '(Or ControlChars.CrLf - gives same result)
and does any of them actually split on Carriage Return followed by Linefeed and only there?
Surprisingly I found that the answer was no to both questions - which prompted me to question: what is a line in a textfile? I've allways assumed it had to end with CrLf.
File.ReadAllLines(filename) will split on Lf, Cr and CrLf (maybe more - haven't tested all controlchars)
File.ReadAllText(filename).Split(Environment.NewLine) seems to get confused and not act rationally at all!
File.ReadAllText(filename).Split() splits on whitespace causing CrLf to leave an undesired Lf string in the array.
File.ReadAllText(filename).Split(New Char() {ControlChars.Cr, ControlChars.Lf}) acts differently than all above.
I tested the examples and wrote my own version, of what I would expect for 'correct behaviour', with the following:
vb.net Code:
Dim filename As String = Path.Combine(Application.StartupPath, "Testing.txt")
File.WriteAllLines(filename, New String() {"Carriage" & ControlChars.Cr & "return", "Nothing", _
"Line" & ControlChars.Lf & "feed", "Spa ce"})
Dim lines() As String
Console.WriteLine("Version 1: ----------------------")
lines = File.ReadAllLines(filename)
For Each line As String In lines
Console.WriteLine(String.Format("'{0}'", line))
Next
Console.WriteLine("Version 2: ----------------------")
lines = File.ReadAllText(filename).Split(Environment.NewLine)
For Each line As String In lines
Console.WriteLine(String.Format("'{0}'", line))
Next
Console.WriteLine("Version 3: ----------------------")
lines = File.ReadAllText(filename).Split()
For Each line As String In lines
Console.WriteLine(String.Format("'{0}'", line))
Next
Console.WriteLine("Version 4: ----------------------")
lines = File.ReadAllText(filename).Split(New Char() {ControlChars.Cr, ControlChars.Lf})
For Each line As String In lines
Console.WriteLine(String.Format("'{0}'", line))
Next
Console.WriteLine("Version 5: ----------------------")
Dim total As String = File.ReadAllText(filename)
Dim i As Integer = 0
Dim length As Integer = 0
While i < total.Length
If total(i) = ControlChars.Lf Then
If i > 0 AndAlso total(i - 1) = ControlChars.Cr Then
Console.WriteLine(String.Format("'{0}'", If(i > 1, total.Substring(i - length, length - 1), String.Empty)))
length = 0
Else
length += 1
End If
Else
length += 1
End If
i += 1
End While
The question was: are lines in a textfile not supposed to be delimited by CrLf (for those who forgot it reading all that sheit)?.
Tom
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|