Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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!

  • Administrators
Posted

The line Do Until (fileRead.Read = -1) is reading the first character in and ignoring it.

 

The following should work

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


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(" ")

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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 wasn't sure which of the 2 was the cause.

Thanks again!

  • *Experts*
Posted

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 don't set it to a value. I also don't see why you're using it on

a variable whose type is already a String.

 

In any case, remove the line strRead.ToString(); it's only slowing down

your code.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...