如果在文件中找到,则用文件中的行替换单词
Replace word with line from file if found on it
我有一个包含大量行的文本文件,格式如下:
word1-word2-word2
另一个词1-另一个词2-另一个词3
differentword1-differentword2-differentword3
等等。我正在尝试做的是,从 richbox 文本的给定文本中循环遍历其中的所有单词,同时我循环遍历文本文件中的每一行。
如果任何一行包含 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
最终代码:
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)
将文本分隔符设置为“|”并将结果输出到 RichTextBox2。 output
是可选的(在本例中为 RichTextBox2
)
现在有了句子支持:)
在文本文件中,如果你写下类似:
I'm a potato-I'm a tomatoe
在节目中:
I'm a tomatoe
或 I'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。抱歉我的英语不好:)
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
这段代码工作正常,没有多次替换问题,没有单词错误...但它只对找到的第一个单词有效...
这会起作用:
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, "-")
有句子支持:)
您遇到的所有错误现在都消失了,而且代码更小了!
如果您希望输出与输入相同的文本框,请告诉我,我会编辑答案(代码会更长)
我有一个包含大量行的文本文件,格式如下:
word1-word2-word2
另一个词1-另一个词2-另一个词3
differentword1-differentword2-differentword3
等等。我正在尝试做的是,从 richbox 文本的给定文本中循环遍历其中的所有单词,同时我循环遍历文本文件中的每一行。
如果任何一行包含 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
最终代码:
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)
将文本分隔符设置为“|”并将结果输出到 RichTextBox2。 output
是可选的(在本例中为 RichTextBox2
)
现在有了句子支持:)
在文本文件中,如果你写下类似:
I'm a potato-I'm a tomatoe
在节目中:
I'm a tomatoe
或 I'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。抱歉我的英语不好:)
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
这段代码工作正常,没有多次替换问题,没有单词错误...但它只对找到的第一个单词有效...
这会起作用:
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, "-")
有句子支持:)
您遇到的所有错误现在都消失了,而且代码更小了!
如果您希望输出与输入相同的文本框,请告诉我,我会编辑答案(代码会更长)