convert file into array of string

bornbroke
09-20-2004, 03:28 PM
I'm trying to read a file and convert it to a array of strings, although it sounds crazy.

Using readline i think i'll be able to read a line of the file, but how do i iterate through the whole file?

I would like to use something like (which doesn't work)
[vb]
dim sArray() as string
dim i as integer

i = 0

if not .EOF then
redim preserve sArray(i) = readline(File)
i = i + 1
end if
[\vb]

but how do i define the start and end of the iteration?

Iceplug
09-20-2004, 08:08 PM
Hopefully, you are using StreamReader.

Use the StreamReader's .ReadToEnd method to get the whole file into a string.

To split the string into an array of strings, use the string's .Split method.

Unfortunately, .Split only takes a single character as a delimiter, so first call .Replace to replace the Environment.NewLine with some character that wouldn't be in the file.

Dim Strgs() As String = Mystring.Replace(Environment.NewLine, "|").Split("|"c)

Here, the string has its new lines replaced with the pipe character, and the string is then split by the pipe character into an array of strings, which go into a string array. :)

bornbroke
09-21-2004, 06:35 AM
Thanks! exactly what I was looking for.
But How do I split it at every line: return character?

sFileCont1 = strRFile1.Split("Chr$(10)")

I thought chr$(10) was the return character, but it seems to be 'c'.
Do i need to include/import a something? Do you know where i can find a table of the characters? I can't find anything in the msdn help library.. thank you.

bornbroke
09-21-2004, 06:47 AM
Actually i've found out "vbCrLf" will do it for me. Thanks again for your input. It was immensly helpful.

Iceplug
09-21-2004, 10:41 AM
I included the line that shows you how to do it in the previous post.

Dim Strgs() As String = Mystring.Replace(Environment.NewLine, "|").Split("|"c)

And I would like to keep you away from the outdated backwards compatibility functions Split() (the version that takes a string as an argument) and vbCrLf (same as Environment.NewLine)
:)

Unless you are using VB6 or an earlier version. :huh:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum