I am reading a text file with mostly alpha characters. the content is not really relevant but the size of each line is very important. The process I will feed this text to will require each line be no more than 50 characters. So I will pre-process the text and add line feeds to make sure that happens.
I tried several VB.NET regex like ^.*$ but that doesn’t really break up the lines by 50 characters. I would take the result and iterate through each match and then cut it up and write it to an object in memory. Can this be done with a single regex pass?
Otherwise I will use a streamreader and on each line check the length and if <=50 write it out with a streamwriter. if >50 cut it up in sections of 50 and then use streamwriter.
A brief example of my text:
119 SMITH KATY AAAA F ZZZ X NB SX ET
MILES,200/LM450
120 JONES THOMAS W QQQ 66-W NB OS SC LW EP
ET
L/G/B/MAY20-2010/JONES/THOMAS/KEITH 121 BUBBA BILLY HH4 S XQT 2PA-F 1 IP SC LH ET
DOCC
122 NEWTON IAASAC S FTY 240-U NB QC LF KD EE
Just looking for tips on how to efficiently do this.
Update: I ended up using the streamreader approach as suggested by SSS. However, I tried to avoid the old Mid function and stick with Substring. Thus i had to make some checks and use some code from another SO post but cant remember which one. anyway here it is:
Dim reader As New StringReader(aSource)
Dim line As String = Nothing
Dim writer As New StringWriter
Dim chunkSize As Integer = 50
Dim chunk As String
Do
line = reader.ReadLine()
If Not String.IsNullOrEmpty(line) Then
Debug.WriteLine(line.Length & "-->" & line)
'if line length is less than or equal to chunk size then write it out, otherwise cut it up and then write the chunks out
If line.Length <= chunkSize Then
writer.WriteLine(line)
Else
Debug.WriteLine("---------------------")
For i = 0 To line.Length Step chunkSize
Debug.WriteLine("i =" & i)
Debug.WriteLine("i+c=" & i + chunkSize)
Debug.WriteLine("L =" & line.Length)
If i + chunkSize > line.Length Then
chunk = line.Substring(i, line.Length - i)
Else
chunk = line.Substring(i, chunkSize)
End If
Debug.WriteLine(" " & chunk.Length & "-->" & chunk)
writer.WriteLine(chunk)
Next i
Debug.WriteLine("---------------------")
End If
End If
Loop While (line IsNot Nothing)
reader.Close()
reader.Dispose()
'this cut string now becomes our source
Debug.WriteLine("==>" & writer.ToString)
sourceText = writer.ToString
writer.Close()
writer.Dispose()
Hope that helps someone with the same problem.
1 Answer