StreamReader/Split Function Problem!

masj78

Active member
Joined
Aug 8, 2003
Messages
31
Location
Harrogate, UK
I am using the Split Function to make a String array of the contence of a text file(array of words). I am reading the file using StreamReader.
The problem occuring is that the first String object in the Array, which is the first word of the text file is missing its first letter.

Example contence of the file is:

This is a test!

The This word gets put into the first array position as his, and all the other words are complete with no letters missing.

My code is as follows:

Dim fileRead As New IO.StreamReader("C:\store.txt")
Dim store As Array

Do Until (fileRead.Read = -1)

strRead = fileRead.ReadToEnd
strRead.ToString()
store = Split(strRead, " ", -1)

Loop


reading out store(0) gives his
Any ideas anyone!
 
The line Do Until (fileRead.Read = -1) is reading the first character in and ignoring it.

The following should work
Code:
Dim fileRead As New IO.StreamReader("C:\store.txt")
Dim store As Array

strRead = fileRead.ReadToEnd
strRead.ToString()
store = Split(strRead, " ", -1)

a slightly tweaked way may be better
Code:
Dim fileRead As New IO.StreamReader("C:\store.txt")
Dim store() As String
Dim strRead as string

strRead = fileRead.ReadToEnd
strRead.ToString()
store = strRead.Split(" ")
 
A star yet again!
Both methods work, although as you mentioned; method 2 is a bit neater. I had a feeling the Do Loop and until clause was a bit dodgy and the ReadToEnd function was only needed, but wasnt sure which of the 2 was the cause.
Thanks again!
 
There is one line of useless code I noticed here. The ToString
method, which every object has, is a function that merely returns
a string representing that object; it does nothing to actually
convert the object to a String type, nor does it do anything if
you dont set it to a value. I also dont see why youre using it on
a variable whose type is already a String.

In any case, remove the line strRead.ToString(); its only slowing down
your code.
 
Back
Top