Netero Netero - 2 months ago 14x Question

Replace word with line from file if found on it

I have a text file with a massive amount of lines with this format:




And so on. And what I'm trying to do is that from the given text from a richbox text to loop through all words from it at the same time I'm looping through each lines from the text file.

And if any line contains the respective word from the richtextbox text then replace the word with the whole line. Like from the word word1 to word1-word2-word3.

I've made a code which sometimes works fine, but not on all words. Just a few of them. So I'd say the code is broken:

For Each Line As String In File.ReadLines(Application.StartupPath + "\test.txt")
Dim thisArray As String() = Line.Split("-"c)
For Each s As String In thisArray
For Each wordfromtext As String In RichTextBox1.Text.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
If wordfromtext = s Then
RichTextBox1.Text = RichTextBox1.Text.Replace(wordfromtext, Line)
End If


This will work:

Public Sub replaceword(ByVal input As RichTextBox, ByVal output As RichTextBox, ByVal textsep As Char)
    output.Text = ""
    Dim tb As New TextBox
    tb.Multiline = True
    tb.Text = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\test.txt")
    Dim int As Integer = 0
    For i As Integer = 0 To input.Lines.Count - 1
        output.Text &= input.Lines(i).ToString & vbNewLine
        repln(output, int, tb.Lines(getextln(input.Lines(i).ToString, textsep)))
        int += 1
    For i1 As Integer = 0 To output.Lines.Count - 2
        Dim inln As String = input.Lines(i1)
        Dim ouln As String = output.Lines(i1)
        If Not ouln.Contains(inln) And ouln.Contains(textsep.ToString) Then
            repln(output, i1, inln)
        End If
End Sub

Public Function getextln(ByVal str As String, ByVal sep As Char) As Integer
    Dim tb As New TextBox
    tb.Multiline = True
    tb.Text = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\test.txt")
    Dim lstb As New ListBox
    For Each l In tb.Lines
    For i As Integer = 0 To lstb.Items.Count - 1
        If lstb.Items.Item(i).ToString.Contains(str) Then
            Dim str1 As String() = lstb.Items.Item(i).ToString.Split(sep.ToString)
            For Each w In str1
                If w = str Then
                    Return i
                    Exit Function
                End If
        End If
End Function

Public Sub repln(ByVal tb1 As RichTextBox, ByVal ln As Integer, ByVal strnew As String)
    Dim str1 As New List(Of String)
    Dim str2 As String = ""
    For Each l In tb1.Lines 'Add each line of input to the list
    str1.RemoveAt(ln) 'Remove the seleted line
    str1.Insert(ln, strnew) 'Fill the gap with the new text
    Dim int As Integer = 0
    For Each l1 In str1 'Output the list
        str2 &= l1 & vbNewLine
    str2 = str2.Substring(0, str2.Length - 2) 'Remove the final line
    tb1.Text = str2 'Send the edited text to the input
End Sub

Call It With:

replaceword(RichTextBox1, RichTextBox2, "-")

With sentence support :)

Every error you had its now gone, and the code is smaller !

If you want the output to be the same textbox as the input, tell it to me and i'll edit the answer (The code will be longer)