如果在文件中找到,则用文件中的行替换单词

时间:2016-07-06 20:26:15

标签: .net vb.net replace line words

我有一个包含大量行格式的文本文件:

WORD1 - 的 WORD2 - 的 WORD2

anotherword1 - 的 anotherword2 - 的 anotherword3

differentword1 - 的 differentword2 - 的 differentword3

等等。而我正在尝试做的是从富文本文本中的给定文本循环遍历所有单词,同时我循环遍历文本文件中的每一行。

如果任何行包含richtextbox文本中的相应单词,则将该单词替换为整行。例如从 word1 word1-word2-word3。

我制作的代码有时可以正常使用,但不是所有单词。其中只有几个。所以我会说代码坏了:

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
            Next
        Next
    Next

3 个答案:

答案 0 :(得分:1)

最终守则:

Public Sub replacewordwithline(ByVal input As RichTextBox, ByVal sep As Char, Optional ByVal output As RichTextBox = Nothing)
    Dim tb As New TextBox 'Create a new textbox
    Dim orgstr As String = input.Text 'Make a backup of the input text
    tb.Multiline = True
    tb.Text = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\test.txt") 'Set the textbox's text to the text file text
    Dim str1 As String() 'Create blank input word list
    Dim str3 As String = ""
    For Each l2 In input.Lines 'For each line in input...
        str3 &= l2 & "#" '...Add a '#' to the end of it
    Next
    str1 = str3.Split({"#"c}, StringSplitOptions.RemoveEmptyEntries) 'Split the input to words
    For Each w1 In str1 'For each word in input
        For Each l1 In tb.Lines 'For each line in text file
            If l1.Contains(w1) Then 'If line contains input word
                Dim str2 As String() = l1.Split({sep}, StringSplitOptions.RemoveEmptyEntries) 'Split text file line
                For Each w2 In str2 'For each word in text file line
                    If w1 = w2 Then 'If input word = text file word
                        repln(input, getln(input, w1), l1) 'Replace line
                    End If
                Next
            End If
        Next
    Next
    Dim bla As Boolean = False
    Try
        If Not output.Name = "" Then 'If output exists
            bla = True
            output.Text = input.Text 'Set the output's text to the input's one
            input.Text = orgstr 'Reset the input's text to the original
        End If
    Catch
    End Try
    If bla = True Then 'Output Exists
        For i1 As Integer = 0 To output.Lines.Count - 1 'For each output line
            Dim inln As String = input.Lines(i1)
            Dim ouln As String = output.Lines(i1)
            If Not ouln.Contains(inln) And ouln.Contains(sep.ToString) Then 'If output line doesn't contain input line and contains the seperator
                repln(output, i1, inln) 'Replace line
            End If
        Next
    ElseIf bla = False Then 'Output Doesn't Exist
        Dim tb1 As New TextBox
        tb1.Text = orgstr
        For i1 As Integer = 0 To input.Lines.Count - 1 'For each input line
            Dim inln As String = tb1.Lines(i1)
            Dim ouln As String = input.Lines(i1)
            If Not ouln.Contains(inln) And ouln.Contains(sep.ToString) Then 'If input line doesn't contain original string line and contains the seperator
                repln(input, i1, inln) ' Replace line
            End If
        Next
    End If
End Sub

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.Add(l)
    Next
    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
    Next
    str2 = str2.Substring(0, str2.Length - 2) 'Remove the final line
    tb1.Text = str2 'Send the edited text to the input
End Sub

Public Function getln(ByVal tb As RichTextBox, ByVal str As String) As Integer
    Dim i As Integer = 0
    For Each l In tb.Lines 'For each line in input
        If l = str Then 'If input line text is the same as the selected string
            Return i 'Return input line number
        End If
        i += 1
    Next
End Function

示例:replacewordwithline(RichTextBox1, "-")会将文本分隔符设置为' - '并将结果输出到RichTextBox1。 replacewordwithline(RichTextBox1, "|", RichTextBox2)会将文字分隔符设置为' |'并将resault输出到RichTextBox2。 output是可选的(在本例中为RichTextBox2

现在有了强烈支持:)

在文本文件中,如果您写的内容如下:

I'm a potato-I'm a tomatoe

关于该计划:

I'm a tomatoeI'm a potato

程序将输出:

I'm a potato-I'm a tomatoe

-取决于分离者。

复制子来电者:

实施例

Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    replacewordwithline(RichTextBox1, "-")
End Sub

Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    replacewordwithline(RichTextBox1, "-")
    replacewordwithline(RichTextBox1, "-")
End Sub

P.S。抱歉我的英文不好:)

答案 1 :(得分:1)

这将有效:

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
    Next
    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
    Next
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
        lstb.Items.Add(l)
    Next
    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
            Next
        End If
    Next
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.Add(l)
    Next
    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
    Next
    str2 = str2.Substring(0, str2.Length - 2) 'Remove the final line
    tb1.Text = str2 'Send the edited text to the input
End Sub

用它打电话:

replaceword(RichTextBox1, RichTextBox2, "-")

支持句子:)

你现在遇到的每一个错误,代码都变小了!

如果您希望输出与输入的文本框相同,请告诉我,我将编辑答案(代码会更长)

答案 2 :(得分:0)

 Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
    Dim LSOS As Integer = Nothing
    Dim linelentgh As Integer = File.ReadAllLines(Application.StartupPath + "\test.txt").Length
    Do Until LSOS >= linelentgh
        Dim line As String = File.ReadLines(Application.StartupPath + "\test.txt").Skip(LSOS).Take(1).First()
        repl(line)
        LSOS += 1
    Loop
End Sub

Sub repl(ByVal line As String)
    Dim thisArray As String() = line.Split("|"c)
    For Each wordfromtext As String In Inputs.Text.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
        For Each s As String In thisArray
            If wordfromtext = s Then
                Output.Text = Inputs.Text.Replace(s, line)
            End If
        Next
    Next
End Sub

此代码工作正常,没有多个替换问题,没有单词错误...但它只适用于它上面的第一个单词...