Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have text file like this

--Start word
--second line
--third line
--fourth line
--fifth line
--End word
--Start word
--second line2
--third line2
--fourth line2
--fifth line2
--sixth line2
--seventh line2
--End word

i want to get result like below

Start word second line third line fourth line fifth line End word
Start word second line2 third line2 fourth line2 fifth line2 sixth line2 seventh line2 End word

What I have tried:

VB
Dim objReader As New System.IO.StreamReader(patefile)
        Dim strTextFile() As String
        Dim Countline As Integer = 0
 
        Do While objReader.Peek <> -1
            ReDim Preserve strTextFileInfo(Countline )
            strTextFileInfo(Countline ) = objReader.ReadLine
            Countline += 1
        Loop
      
        Dim startStr, EndStr As String
        startStr = "Start word"
        EndStr = "End word"

        For i As Integer = 0 To arrCounter - 1
            If strTextFileInfo(i).StartsWith(startStr) Then
                ListBox1.Items.Add(strTextFileInfo(i))

            End If
        Next
Posted
Updated 3-Aug-20 1:50am
v2
Comments
Patrice T 3-Aug-20 4:49am    
And the problem is ?
Nelek 3-Aug-20 4:59am    
and what happens with the "--" at the beginning of each line? Your input, your description and your desired output don't match

1 solution

Read all the text into a single string - File.ReadAllText will do that - and then use the Replace and Split methods:
Dim chunks As String() = allLines.Replace("--", "").Split(New String() {"Start word", "End word"}, StringSplitOptions.RemoveEmptyEntries)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900