masj78 Posted September 1, 2003 Posted September 1, 2003 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! Quote
Administrators PlausiblyDamp Posted September 1, 2003 Administrators Posted September 1, 2003 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(" ") Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
masj78 Posted September 1, 2003 Author Posted September 1, 2003 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! Quote
*Experts* Bucky Posted September 1, 2003 *Experts* Posted September 1, 2003 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Administrators PlausiblyDamp Posted September 2, 2003 Administrators Posted September 2, 2003 Never even saw that in the code! Should really pay more attention ;) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
masj78 Posted September 2, 2003 Author Posted September 2, 2003 Will do! Cheers! Didn't notice that! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.