Problem with IO file and line splitting with Environment.NewLine in VB.Net

I experimented with the base section of VB.Net File IO and String. I ran into this problem. I do not know if this is related to the File IO or String section.

I write text to such a file

Dim sWriter As New StreamWriter("Data.txt") sWriter.WriteLine("FirstItem") sWriter.WriteLine("SecondItem") sWriter.WriteLine("ThirdItem") sWriter.Close() 

Then I read the text from the file

 Dim sReader As New StreamReader("Data.txt") Dim fileContents As String = sReader.ReadToEnd() sReader.Close() 

Now I am splitting fileContents using Environment.NewLine as a delimiter.

 Dim tempStr() As String = fileContents.Split(Environment.NewLine) 

When I print the resulting array, I get some weird results

 For Each str As String In tempStr Console.WriteLine("*" + str + "*") Next 

I added * s to the beginning and end of array elements during printing to find out what was going on. Since NewLine used as a delimiter, I expected that the rows in the array will NOT have a new line, NewLine. But the way out was this -

 *FirstItem* * SecondItem* * ThirdItem* * * 

It should not be this -

 *FirstItem* *SecondItem* *ThirdItem* ** 

??

Why is there a new line at the beginning of all but the first line?

Update: I made a character with a character print fileContents and got this -

 F - 70 i - 105 r - 114 s - 115 t - 116 I - 73 t - 116 e - 101 m - 109 - 13 - 10 S - 83 e - 101 c - 99 o - 111 n - 110 d - 100 I - 73 t - 116 e - 101 m - 109 - 13 - 10 T - 84 h - 104 i - 105 r - 114 d - 100 I - 73 t - 116 e - 101 m - 109 - 13 - 10 

It seems that "Environment.NewLine" consists of

  - 13 - 10 

13 and 10 .. I understand. But the empty space between them? I do not know if this will be due to printing to the console or is it really part of NewLine .

Thus, when splitting, the separator (as explained in the answers) uses only the equivalent of the ASCII value character 13, which is the first character of NewLine , and the remaining elements are still present in the lines. For some reason, the mysterious empty space in the list above and the ASCII value of 10 together cause a new line to print.

Now it is clear. Thanks for the help.:)

+6
string file-io
source share
3 answers

First of all, yes, WriteLine encloses a new line at the end of the line, hence the empty line at the end.

The problem is how you call fileContents.Split() . The only version of this function that takes only one argument accepts char() , not string . Environment.NewLine is a string, not a char , so (assuming you have Option Strict Off ) when you call a function, it implicitly converts it to char using only the first character in the string. This means that instead of dividing your string into the actual sequence of the two characters that make up the Environment.NewLine , it actually only splits into the first of these characters.

To get the desired result, you need to call it like this:

 Dim delims() as String = { Environment.NewLine } Dim tempStr() As String = fileContents.Split(delims, _ StringSplitOptions.RemoveEmptyEntries) 

This will cause it to split into the actual string, rather than the first character, as it is now, and remove all empty records from the results.

+6
source share

Why not just use File.ReadAllLines ? One single call reads the file and returns a string array with strings.

 Dim tempStr() As String = File.ReadAllLines("data.txt") 
+3
source share

I just ran into the same problem and found all the comments very helpful. However, I fixed my problem by replacing "Environment.NewLine" with vbLF (unlike vbCrLf, which had the same problem). Any problems with this approach? (This seems more direct, but I'm not a programmer, so I don't know any potential problems).

0
source share

All Articles