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.:)