I am writing a program that reads a file and then displays the words in a listbox. What I am having trouble with is that I need to also display the line that the word was on in the file. Could someone please push me in the right direction for this.
Printable View
I am writing a program that reads a file and then displays the words in a listbox. What I am having trouble with is that I need to also display the line that the word was on in the file. Could someone please push me in the right direction for this.
try this:
Code:Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lines() As String = IO.File.ReadAllLines("filename.txt")
For x As Integer = 0 To lines.GetUpperBound(0)
For Each m As Match In New Regex("\b\w+\b").Matches(lines(x))
ListBox1.Items.Add(String.Format("{0}: {1}", x, m.Value))
Next
Next
End Sub
End Class
I think I figured that part out but maybe you can help me with something else. I read the file and did all that I was supposed to and then I wrote that to another text file but now I can't figure out how to save that file with the same name as the one I read with the word "Concordance" appended to the front of it. Thank you for helping me so far.
Code:dim filename as string = "filename.txt"
dim path as string = "c:\test"
dim combinedPath1 as string = io.path.combine(path, filename) ' = "c:\test\filename.txt"
dim combinedPath2 as string = io.path.combine(path, "Concordance" & filename) ' = "c:\test\Concordancefilename.txt"