I have a bit of vb.net code that will search for a string, then get a certain amount of characters after it and display in within a text box.

My issue is that is some cases there are more than one string of text I need to display, so rather than just searching for "name" once, how can I loop through the file and display a list of all instances?

This is the code I am using now.

Public Sub readAddress()

Dim sr As StreamReader = New StreamReader(profile + "\Desktop\" + dir + "\" + newfile + ".dat")
Dim data = sr.ReadToEnd()
Dim pos = data.IndexOf("name")
        If pos >= 0 Then
            ListBox1.Text = data.Substring(pos, 39).Replace("name""", "") + Environment.NewLine
        End If
sr.Close()
txt()

Since you're already reading the whole file into memory, you can use the Split method to create an array of strings, then iterate through the array to add each item to your listbox. depending on the exact format of your data you may need to tweak things but the basic idea should work
Something like this might work:

Dim data As String= File.ReadAllText(profile + "\Desktop\" + dir + "\" + newfile + ".dat")
'using a substring starting right after the first "name", keeps everything before that out of the array
For Each subdata As String In data.Substring(data.IndexOf("name") + 4).Split(New String(){"name"},StringSplitOptions.RemoveEmptyEntries)
    ListBox1.Items.Add(subdata.SubString(0,35))
Nest
sr.Close()

Thanks for your post, im not all that sharp on vb.net, I have a question.

What does it mean when "'ReadAllText' is not a member of 'string'"?

EDIT:

This works perfectly thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.