String array problem

P

Paul

Hi,

I'm trying to create an array containing the entire contents of a text file
and delimit the elements using vbCrLf.

The text file is like this....

-text1
-text2
-text3
etc...

I can read the file in like this...
Dim TEST As New StreamReader("C:\test.txt")
Dim line As String = TEST.ReadLine()
While Not line = Nothing
Console.WriteLine(line)
line = TEST.ReadLine()
End While

What i'm trying to do is load each element of the text file, in this case
each line (-text1, -text2, -text3 etc..) into an array which I can later use
to compare another string (let's say, strA) with and remove, if found, any
occurances of an array element from strA. So for example, if strA contains
"this is a string -line2" then the "-text2" part of is removed from strA
using strA = strA.Replace(Whatever match was found, "")

All I have at the moment is how to read the file in, as i wrote above, so
any help with the creation of the array and the comparing against another
string is much appreciated.

Cheers,
Paul
 
S

Stephany Young

Try something like:

Dim _sr As StreamReader = New StreamReader("C:\test.txt")

Dim _ss() As String = _sr.ReadToEnd().Replace(ControlChars.Lf,
String.Empty).Split(ControlChars.Cr)

_sr.Close()

For _i As Integer = 0 to _i.Length - 1
Dim _p As Integer = _strA.IndexOf(_ss(_i))
If p > -1 Then _strA = _strA.Remove(_p, _ss(_i).Length)
Next
 
P

Paul

Hey, thanks for the reply Stephany.

I'm getting an error on your code on this line though.
For _i As Integer = 0 To _i.Length - 1
'Length' is not a member of 'Integer'

I changed that to _i.ToString.Length -1 and it compiled but only the matches
of the first line of the array are removed.
For example if strA contains -line1 then -line1 is removed but if strA
contains -line2 or anything else the match is not removed.


Thanks,
Paul
 
P

Phill. W

Paul said:
I'm trying to create an array containing the entire contents of a text file
and delimit the elements using vbCrLf.

Read the entire file into a String ([StreamReader].ReadToEnd),
then use

Imports Microsoft.VisualBasic
. . .
<array> = [Strings].Split( sFiledata, vbCrLf )

to break it up into lines.

Beware [String].Split(), which /only/ supports single-character delimiters.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top