reading a simple text file, splitting and sorting the contents using vb

13,302

It depends what you want do do with the text you split really.

The Split() function will return you an array of string with the results of your split, from there it really depends on the data.

Here is an example of using split http://www.dotnetperls.com/split-vbnet

Since you mentioned you want to sort the data alphabetically you may wish to look at http://www.codepedia.com/1/VBNET_ArraySort or look into using LINQ.

It is quite acceptable to nest a loop within your main loop if you want to do something more complex with the data.

Share:
13,302
Craig Hickey
Author by

Craig Hickey

Updated on November 21, 2022

Comments

  • Craig Hickey
    Craig Hickey over 1 year

    I'm quite new to vb and doing simple basics, I have managed to access and read a specific file line by line. If I wanted to split information by a comma or space and then sort alphabetically or numerically, how would I go about this procedure? Would I create a loop within the reading loop to parse the information? A simple to follow example would really help...Thanks!

    Dim file As String = "C:\Users\test.txt"
        Dim Line As String
    
    If System.IO.File.Exists(file) = True Then
            Dim objReader As New System.IO.StreamReader(file)
            Do While objReader.Peek() <> -1
                Line = Line & objReader.ReadLine() & vbNewLine
    Loop
    Next
    Label1.Text = Line
    
     objReader.Close()
     Else
     MsgBox("File Does Not Exist")
    
        End If
    
  • Craig Hickey
    Craig Hickey over 12 years
    Thanks for those pointers, the example looks simple to follow so I'll give it a try! I just wanted to split text that was possibly on one line and return the individual items onto separate lines.
  • Purplegoldfish
    Purplegoldfish over 12 years
    @CraigHickey Good luck, it should be fairly simple to do what you want I think