Skip to content

Breaking a line in a file..

recca hanabishi

Asked May 16, 2006, 9:48 PM EDT1 answer
Share
LinkedInX
 good day
 
   anyone out there knows how to break a line in a file?

for example: i have this file name "sample.txt" and contains

fjdslfjlkdsjf;fsdafdsffdsdsasd;fasdfsdf;0;fsdaf
fsdaffdsffd;fsdafdsfafdssdas;fasdfds;9;fdsfsd.

What i want to do is.. i want to break the first line whenever
the system meets the semicolon , and populate
its values to the listview control.

im looking for a function..

my code:

dim f as new filestream("sample.txt", filemode.open, fileaccess.read
dim r as new streamreader(f)

dim lv as new listviewitem
r.basestream.seek(0,seekorigin.begin)
while r.peek()>-1
     lv=lst.items.add(r.read().Tochararray(0,13))  //this part i break the first 13 items
     lv.subitems.add(r.read().substring(14,16)) //but this gets only 15 chars from offset, 
                                                                  and then prompts an error        
end while


any help is appreciated.

thanx,

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    First, when reading the file you have many options.  You can read the entire file in one shot (like below).  You could read a single line, by using StreamReader.ReadLine.  It is up to you. 

        Public Function FileToString(ByVal FilePath As String) As String
            If File.Exists(FilePath) Then
                Dim objReader As StreamReader
                objReader = File.OpenText(FilePath)
                FileToString = objReader.ReadToEnd
                objReader.Close()
                objReader = Nothing
            End If
        End Function

    Second, to segment a string into an array of strings, use the String.Split method, which returns a string array.

    Dim ExampleData as String = "0 element;1 element;2 element"
    Dim Segments() as String = ExampleData.Split(";")

    Segments(0) will be "0 element", Segments(1) will be "1 element", etc.
    0