Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with File IO and splitting strings with Environment.NewLine in VB.Net

I was experimenting with basic VB.Net File IO and String splitting. I encountered this problem. I don't know whether it has something to do with the File IO or String splitting.

I am writing text to a file like so

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

Then, I am reading 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 the 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 the *s to the beginning and end of the Array items during printing, to find out what is going on. Since NewLine is used as the delimiter, I expected the strings in the Array to NOT have any NewLine's. But the output was this -

*FirstItem*
*
SecondItem*
*
ThirdItem*
*
*

Shouldn't it be this -

*FirstItem*
*SecondItem*
*ThirdItem*
**

??

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

Update: I did a character by character print of 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 'Environment.NewLine' consists of

 - 13

 - 10

13 and 10.. I understand. But the empty space in between? I don't know whether it is coming due to printing to the console or is really a part of NewLine.

So, when splitting, only the character equivalent of ASCII value 13, which is the first character of NewLine, is used as delimiter (as explained in the replies) and the remaining stuff is still present in the strings. For some reason, the mysterious empty space in the list above and ASCII value 10 together result in a new line being printed.

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

like image 974
Senthil Avatar asked Apr 10 '10 14:04

Senthil


People also ask

How do I split a string into a newline?

Split String at Newline Split a string at a newline character. When the literal \n represents a newline character, convert it to an actual newline using the compose function. Then use splitlines to split the string at the newline character. Create a string in which two lines of text are separated by \n .

How do you split a string in VB net?

How to VB.NET String. Split() The VB.Net Split() extracts the substrings from the given string that are delimited by the separator parameter, and returns those substrings as elements of an array. If your String contains "dd-mm-yy", split on the "-" character to get an array of: "dd" "mm" "yy".

How split a string after a specific character in C#?

Split(Char[], Int32, StringSplitOptions) Method This method is used to splits a string into a maximum number of substrings based on the characters in an array. Syntax: public String[] Split(char[] separator, int count, StringSplitOptions options);


1 Answers

First of all, yes, WriteLine tacks on a newline to the end of the string, hence the blank line at the end.

The problem is the way you're calling fileContents.Split(). The only version of that function that takes only one argument takes a char(), not a string. Environment.NewLine is a string, not a char, so (assuming you have Option Strict Off) when you're calling the function it's implicitly converting it to a char, using only the first character in the string. This means that instead of splitting your string on the actual sequence of two characters that make up Environment.NewLine, it's actually splitting only on the first of those characters.

To get your desired output, 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 on the actual string, rather than the first character as it's doing now, and it will remove any blank entries from the results.

like image 140
Adam Robinson Avatar answered Nov 22 '22 17:11

Adam Robinson