parse file

I have a file that looks like ISA*00**00*27*00523*ZZ*M4788
Could you give me example code on how to parse this file that has * seperating elements?





lindapatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
Private Sub Command1_Click()
Dim ff As Integer, Ln As String, arrLn() As String
ff = FreeFile
Open "C:\Myfile.txt" For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
' split line into array by "*"
arrLn = Split(Ln, "*")

' loop through array
Dim i As Integer
For i = 0 To UBound(arrLn)
    If arrLn(i) <> "" Then MsgBox arrLn(i)
Next i

DoEvents
Loop
Close #ff

End Sub
0
learning_t0_pr0gramCommented:
TheFile = your file

'==code==
Dim tmp() as string

tmp() = split(TheFile, "*")

For a = 0 to ubound(TheFile)
   Debug.Print tmp(a)
Next
0
lindapatAuthor Commented:
I am new at VB.net so please excuse me.  Will this code work? If not would you correct it for me? I want to show each element in label I have created on a form.
Private Sub read835()
        Dim isaLine As String
        Dim elementSep, compositeSep, segmentSep As String
        Dim x As Integer
        Dim tmp() As String
        isaLine = "ISA*00*          *00*          *27*00523          *ZZ*M4788          *040414*2351*U*00401*410500028*0*P*:~"
        elementSep = isaLine.Substring(103, 1)

        For x = 0 To isaLine.Length
            tmp = Split(isaLine, "*")

        Next
        lblMsg.Text = tmp(x)
0
AllpaxCommented:
For x = 0 To isaLine.Length
            tmp = Split(isaLine, "*")
Next
^
This is wrong because the split function acts like a loop itself and parses the string.  All you would need is

    tmp() = Split(isaLine, "*")
       
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
learning_t0_pr0gramCommented:
yes but then there will be blank lines.. for instance..

if you split it by * it will look like this:

tmp(0) = "ISA"
tmp(1) = "00"
tmp(2) = "          "
tmp(3) = "00"
tmp(4) = "          "
tmp(5) = "27"
tmp(6) = "00523"
tmp(7) = "ZZ"

etc...

so which lines do you want to get out??  if you do it like i had it, u could get all the information out and check if it's all spaces, and if it is skip it.. such as:


Dim tmp() as string

tmp() = split(TheFile, "*")

For a = 0 to ubound(TheFile)
   if Trim(tmp(a)) <> "" Then MsgBox tmp(a)
Next

ubound fwould be the uppermost index in the array, and trim replaces spaces on the ends of the string, so trim("       ") would be the same as ""
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.