Parse a text file by line
I'm running into a problem whenever I try to parse a text file by each line. I know I could use stream reader to read line by line but it is a lot easier to simply use split() and I would also like to know the reason why split() doesn't work.
For example, I created a file "test.txt" and filled it with the following text.
then put the following code in the load event of the form (a button click would work the same).
Code:
Dim str As String = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\test.txt")
MsgBox("testforline" & str.Split(Environment.NewLine)(1))
The result is text2 appearing on the line below testforline. When using split, split is supposed to remove the character that was dividing the information as well however this isn't the case with new lines. How can I fix this? Also I tried using cchar() on environment.newline and controlchars.newline and neither worked.
Re: Parse a text file by line
The reason that Split doesn't work is that the overload you're using splits on a Char, i.e. a single character, not on a String. Environment.NewLine is a String that contains two characters, i.e. CR and LF. In your code, that is being implicitly converted to a Char, i.e. CR, so the LF gets left behind in the data.
First, you must have Option Strict Off or that code wouldn't even compile. Turn Option Strict On and leave it on and then you won;t ever be surprised by implicit conversions like that.
There is an overload of String.Split that lets you split on substrings rather than characters, but you don;t need to use it anyway. The easy option here is:
vb.net Code:
For Each line As String In IO.File.ReadAllLines("file path here")
Re: Parse a text file by line
I always use option strict on but I put together this example rather than the exact code I was using before and left it out. Also, what if the text I want is in a string? Is there an easier way to read a string line by line without having to write it to a file and read it from there?
Re: Parse a text file by line
vb Code:
Dim lines() As String = Str.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
Re: Parse a text file by line
Quote:
Originally Posted by
Vectris
I always use option strict on but I put together this example rather than the exact code I was using before and left it out.
There shouldn't be any possibility of "leaving it out". You should have set Option Strict On in the IDE options and then it will be On by default for every project you create. You would then have to specifically set it Off at the project or file level if you wanted it Off, which you almost never would.
Re: Parse a text file by line
Quote:
Originally Posted by
jmcilhinney
There shouldn't be any possibility of "leaving it out". You should have set Option Strict On in the IDE options and then it will be On by default for every project you create. You would then have to specifically set it Off at the project or file level if you wanted it Off, which you almost never would.
I was not aware you could do that, I'll definitely change it.