Solved

VB.NET - Process a block of text line-by-line

Posted on 2015-02-11
1
259 Views
Last Modified: 2015-02-11
I have blocks of text that I need to process line-by-line. I'm accustomed to using the FileStream and ReadLine methods, but those work on files read from disc, it seems, and I can't translate that over to my need.

The textblocks look something like this, although they are much, much larger in the real world:
//STX12//830CATMF                                           SC
13MF CRTT Dir Global
14Z Z
15+00000000002C150306
15+00000000001D150317
15+00000000001D150416
//STX12//830MORTON      20150210064507 0411                            CAR
14Y Z
15+00000000028C150715
//STX12//830MORTON      20150210064506 0411                            CAR
01050411                          SH150401151202150209HESO51218
14Y Z
15+00000000040C150401
15+00000000040C150422
15+00000000040C150722

Open in new window

I'm breaking them on the lines beginning with "//STX12//", and I need to read each of those sub-blocks line-by-line. I see where .NET Framework 4.5 has the Text.ReadLines method, but I cannot require my client to upgrade to that framework.

What's the most efficient way to read a block of text line by line? Can I somehow "insert" that block of text into a FileStream object, and then use the methods there?
0
Comment
1 Comment
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 0 total points
ID: 40602974
I found a way to do this using the StringBuilder class:
'/ Read the file from disc
Dim fil As New StreamReader(FileIn)
Dim file As String = fil.ReadToEnd
'/ Break it into chunks
Dim files() As String = Strings.Split(file, "//STX12//")
  '/ loop through the "chunks"
  For i As Integer = 0 To files.GetUpperBound(0)
	'/ get the first "chunk" into a string variable
    Dim edifile As String = files(i)
    If edifile.Length > 0 Then
		'/ use StringReader to get the "chunk" into the object
        Dim block As New StringReader(edifile)
		'/ use methods of the StringReader object to move through the lines
        Do Until block.Peek = -1
            Dim s As String = block.ReadLine
			< do stuff with line here>
		Loop
	End If
Next i

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error on Add method 1 38
Change Format on Data when exporting to Excel 4 23
Close tabpage of custom control 9 23
Help with LINQ and XML 10 27
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now