Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Read and split components of a file line
 

Read and split components of a file line

Hi,

I have a problem I was hoping someone might be able to help with please. I am trying to read and interpret a .txt filewhich typically has a data line of the form:

DESCRIPTIONNAME 104.19 6171 1.27 2.36 140 8.45

I want to be able to split each value, i.e. so I have:

DESCRIPTIONNAME
104.19
6171
1.27
2.36
140
8.45

Each component is not comma or tab separated (and the no' of spaces varies between components) - how can my code know how to split them up?

If anyone can help, I would be extremely grateful.

Thanks

Jon

p.s. I'm using VB Express. Thanks again.
jlb298  Thursday, November 26, 2009 9:15 PM
You could use Replace to replace every occurrence of two spaces with a single space, and then use Split.

Or, you could usesplit with the no empt entriesoption. Multiple spaces will be treated as one separator.

        Dim SepChar() As Char = {" "c}
        Dim Words() As String = Line.Split(SepChar, System.StringSplitOptions.RemoveEmptyEntries)

  • Marked As Answer byjlb298 Friday, November 27, 2009 6:56 AM
  •  
Acamar  Thursday, November 26, 2009 9:39 PM
    Dim S As String = "DESCRIPTIONNAME     104.19       6171    1.27       2.36        140    8.45"
    Dim Fields() As String = S.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)

  • Marked As Answer byjlb298 Friday, November 27, 2009 6:56 AM
  •  
JohnWein  Thursday, November 26, 2009 9:44 PM
You could use Replace to replace every occurrence of two spaces with a single space, and then use Split.

Or, you could usesplit with the no empt entriesoption. Multiple spaces will be treated as one separator.

        Dim SepChar() As Char = {" "c}
        Dim Words() As String = Line.Split(SepChar, System.StringSplitOptions.RemoveEmptyEntries)

  • Marked As Answer byjlb298 Friday, November 27, 2009 6:56 AM
  •  
Acamar  Thursday, November 26, 2009 9:39 PM
    Dim S As String = "DESCRIPTIONNAME     104.19       6171    1.27       2.36        140    8.45"
    Dim Fields() As String = S.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)

  • Marked As Answer byjlb298 Friday, November 27, 2009 6:56 AM
  •  
JohnWein  Thursday, November 26, 2009 9:44 PM
Thanks Acamar, that's a great idea and is very helpful.

Thanks again, Jon
jlb298  Friday, November 27, 2009 6:58 AM
Thanks John also, the split and remove empties is a perfect solution.

Thanks again, Jon
jlb298  Friday, November 27, 2009 6:59 AM

You can use google to search for other answers

Custom Search

More Threads

• Passing info across threads
• Catch cscript Input
• Secure String Help
• What's better. using an array to hold a range of data, or more rows in a table?
• Automatic login in WebBrowser
• What happen when we register vb6 dlls
• Opening one form from another
• passing page behind code to a vb.net class library
• How to pass a date parameter in Crystal Reports
• How to Modify Edit Boxes in Another App?