且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何搜索字符串并动态替换特定单词?

更新时间:2022-05-06 06:13:48

这是我对它的看法。

Here's my take on it.
For Each textpath As String In Files.FileNames

           RichTextBox1.Clear()

           Dim newline = String.Empty
           Dim regstr = String.Empty

           //Process each line in the file once
           For Each line As String In File.ReadLines(textpath)
              newline = line
              //apply each replacement rule to the result of the previous replacement rule
              For I As Integer = 0 To repdt.Rows.Count - 1
                  If newline.Contains(repdt.Rows(I).Item("originaltext").ToString) Then
                       newline = newline.Replace(repdt.Rows(I).Item("originaltext"), repdt.Rows(I).Item("replacementtext"))
                  
                  //hold over code?
                  //regstr = String.Join(Environment.NewLine, newline.Split(New Char() {ControlChars.Lf}, StringSplitOptions.RemoveEmptyEntries)).Replace(vbTab, "")


                   End If
               Next
               //remove linefeeds and tabs
               newline=newline.Replace(Environment.NewLine,"").Replace(vbTab, "")
               //after all rules, add final result to the textbox
               if newline.TrimEnd().Length>0 then
                   RichTextBox1.Text += newline + Environment.NewLine
               end if
           Next