Re: String builder (Parsing vertically presented records)



Hi,

Another implementation :

===================
Private Sub ReadText()
Dim fsr As New FileStream("VertFile.txt", FileMode.Open,
FileAccess.Read)
Dim fsw As New FileStream("ResultFile.txt", FileMode.Create,
FileAccess.Write)
Dim sr As New StreamReader(fsr)
Dim sw As New StreamWriter(fsw)

Dim thisLine As String
Dim lineContents() As String

'Ignore the first line of the text file
sr.ReadLine()
While sr.Peek > -1
thisLine = sr.ReadLine()
lineContents = thisLine.Split(New Char() {" "c})
If lineContents(1).Trim = "NewRegID" Then
'We have a new record. Write current value of sb and Insert new
line.
If sb.Length > 0 Then
'Replace the last comma with a period.
sb.Replace(",", ".", sb.Length - 1, 1)
sw.WriteLine(sb.ToString())
End If
sb = New StringBuilder()
sb.Append(lineContents(0))
sb.Append(",")
Else
'This is the same record. Keep adding text
sb.Append(lineContents(1))
sb.Append(",")
If sb.Length > 2 Then
sb.Append(lineContents(2))
sb.Append(",")
End If
End If
End While
'Replace the last comma with a period.
sb.Replace(",", ".", sb.Length - 1, 1)
sw.WriteLine(sb.ToString())
sw.Flush()
'Clean up
sw.Close()
sr.Close()
fsr.Close()
fsw.Close()
End Sub
===================

Note that your third set of records (213565432) does not have the line
"NewRegID".
When that line is inserted in the sample text, then it works.

Regards,

Cerebrus.

.



Relevant Pages

  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)
  • Auto Write Name and Merge across
    ... Dim Sheetname01 As String ... Dim WeekName01 As String ...
    (microsoft.public.excel.misc)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Help answer these 70-310 questions
    ... One argument is the string ... Dim output As New StringBuilder ... EmployeeLocations. ... You create a strongly named serviced component. ...
    (microsoft.public.cert.exam.mcsd)

Loading