-
March 26th, 2014, 01:57 AM
#1
Parsing a text file in vb2013
Im trying to parse a text file (sample line from the text file is below). As you can see, all of the fields in the text line are separated by a space, and I have managed to parse that using the code below...and as a result, I can get all space separated values in a text box array. But the first 2 fields are separated by '|' instead of a space.
I have 2 questions.
1) How can i parse a line based on 2 separators in the same line? (| and space)
2) Frankly, I dont even need the text before |, so essentially I can begin reading the line after the |. How can i accomplish that?
Thanks for the help!
Code:
MON|MIKE TRUE 1 1500 68.8 1 1000 68.75 1 500 68.51 1 500 68.5
Code:
If line IsNot Nothing Then
Dim strings = line.Split(New String() {" "}, StringSplitOptions.None)
Array.ForEach(Enumerable.Range(0, textboxes.Length).ToArray, Sub(x) textboxes(x).Text = strings(x))
End If
-
March 27th, 2014, 02:17 AM
#2
Re: Parsing a text file in vb2013
see Substring and indexof
Basically you want to split the substring starting 1 position past the |
You could also do 2 splits, which will give you two elements in the resulting array then you can split that second element on space to get the results you want
Always use [code][/code] tags when posting code.
-
March 27th, 2014, 12:29 PM
#3
Re: Parsing a text file in vb2013
Thanks Datamiser. I used 2 splits to achieve the desired result. Thanks for your help.
Now am stuck at another junction. Im trying to loop the process, as I want to continuously read the file, since its frequently updated. Have tried using the Do While loop in various ways , but the app freezes every time. Any logical way of reading the file continually without freezing it up? Where exactly would i apply the loop?
Code:
Dim line = IO.File.ReadAllLines("C:\Users\Administrator\Desktop\linedata.txt").LastOrDefault
If line IsNot Nothing Then
Dim string_i = line.Split(New String() {"|"}, StringSplitOptions.RemoveEmptyEntries)
Dim string_f = string_i(1).Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
Array.ForEach(Enumerable.Range(0, textboxes.Length).ToArray, Sub(x) textboxes(x).Text = string_f(x))
End if
-
March 27th, 2014, 01:50 PM
#4
Re: Parsing a text file in vb2013
Rather than reading it continuously you may want to consider using the file system watcher. This can cause an event to fire when the file is changed and trigger your code to read the file.
You could also use a timer and fire the read every xxx ms I would prefer the file watcher and I would not use a loop.
Always use [code][/code] tags when posting code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|