tags:

views:

180

answers:

2

Hi,

I have to text file in the following format :

Word[tab][tab]Word[Carriage Return]
Word[tab][tab]Word[Carriage Return]
Word[tab][tab]Word[Carriage Return]

I want to get all the words before the tab into one array or to create a new text file and the all the words after the tab into another array or create a new text file too.

Here my function to get the words before tab into an array :

Protected Sub MakeWordListBeforeTab()

    Dim filename As String = "D:\lao\00001.txt"

    'read from file'
    Dim MyStream As New StreamReader(filename)

    'words before tab
    Dim WordBeforeTabArr() As String = MyStream.ReadToEnd.Split(CChar("\t"))
    MyStream.Close()

    'test to see the word in array
    For d As Integer = 0 To WordBeforeTabArr.Length - 1
        MsgBox(WordBeforeTabArr(d))
    Next

End Sub

I wrote the above function to get all words before tab but I got all the words into array. I've been trying to use the Split method above. What is another method to split those words ? Can anyone show me some code to get this done right ?

I know this can be done with regular expression but I don't know regex yet. If you can show me how to get this done with regex it'll be awesome. Thanks.

A: 

You could try the split function on String. It could be used like this:

Dim lines() As String = IO.File.ReadAllLines(filename)
For Each line As String In lines
    Dim words() As String = _
     line.Split(New Char() {vbTab}, StringSplitOptions.RemoveEmptyEntries)
Next

The words array for each line would the two words. One word at each position. You could fill your two arrays or write the values out to a text file or file as you split the lines of the input file in the loop.

Guster_Q
Yeah, it works. Thanks.
Angkor Wat
A: 

First of all above code is not compiling: See proper code as follows:

Dim lines() As String = IO.File.ReadAllLines(test_Filename)
For Each line As String In lines
  Dim words() As String = _
   line.Split("\t".ToCharArray()(0), StringSplitOptions.RemoveEmptyEntries)
Next