Write line in text file

Bigbadborris

Active member
Joined
Aug 21, 2013
Messages
35
Programming Experience
Beginner
Hi all

I have a listbox wich displays the contents of a part build list the data is formatted as below

234c - DRIVE SHAFT
1C - GEARBOX CASTINGS
35A - DRIVE BEARING
1235A - PUMP

I have worked out how to extract the 1st 5 characters which is the part number from each line, trim the blank spaces. I would then like to write the part number to a text file. with all the part numbers in the build list on one line.
i.e 234C,1C,35A,1235A
when a new build list is written it needs to do the same but on the next line in the text file.

This is what I have so far but i can either get it to append the next build list of to the end of the 1st line entered, or it writes each part number on a seperate line

VB.NET:
 Dim ObjWriter As New System.IO.StreamWriter(File_Name, True)

        For Each Item As Object In lstBuildlist.Items
            BuildPart = (Item)
            BuildPartSplit = BuildPart.Substring(0, 5).Trim
            ObjWriter.Write(BuildPartSplit & ",")
        Next

        ObjWriter.Close()

where am I going wrong
Many Thanks
 
That code you have is wrong. This:
VB.NET:
BuildPartSplit = BuildPart.Substring(0, 5).Trim
is going to give you "1C -" and "35A -" for the second and third items. The way to get those part numbers would be:
VB.NET:
BuildPartSplit = BuildPart.Split("-"c).TrimEnd()
If you're confident that every item will have a single space before the dash then you can drop the trim and incorporate it into the spit:
VB.NET:
BuildPartSplit = BuildPart.Split({" - "}, StringSplitOptions.None)
As for the question, you can put the part numbers into a List(Of String) as you loop and then, at the end, you can use String.Join to put them all together:
VB.NET:
Dim partNumbers As String = String.Join(",", myList)
 

Latest posts

Back
Top