Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working on replacing words in a text before showing all text in a richtextbox. The issue I am having is that the code seems to be reiterating through the loop, instead of a bulk text replacement.

here is my code:

text file path connection string
sql connection with using select * to fill datata table(repdt)

For Each textpath As String In Files.FileNames

           RichTextBox1.Clear()

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

           For I As Integer = 0 To repdt.Rows.Count - 1
               For Each line As String In File.ReadLines(textpath)
                   If line.Contains(repdt.Rows(I).Item("originaltext").ToString) Then
                       newline = line.Replace(repdt.Rows(I).Item("originaltext"), repdt.Rows(I).Item("replacementtext"))
                         regstr = String.Join(Environment.NewLine, line.Split(New Char() {ControlChars.Lf}, StringSplitOptions.RemoveEmptyEntries)).Replace(vbTab, "")

                       RichTextBox1.Text += newline + Environment.NewLine
                   End If
               Next
           Next



I am trying to make this into a library or text to replace.

What I have tried:

Initially, I was using this:

For Each textpath As String In Files.FileNames
           RichTextBox1.Clear()

           Dim regstr As String = String.Join(Environment.NewLine, System.IO.File.ReadAllText(textpath).Split(New Char() {ControlChars.Lf}, StringSplitOptions.RemoveEmptyEntries))

           Dim replacestr As String = regstr.Replace("text", "text").Replace("text", "text").Replace("text", "text"). 'etc

           RichTextBox1.Text = replacestr
       Next


however, this requires coding everytime I want to replace something.
Posted
Updated 10-Sep-18 8:30am
v2
Comments
Ralf Meier 10-Sep-18 9:51am    
From where does the replace-string-parts come from ?
If you want to have a library-method - how do you want to tell it what it should do ?
Member 11856456 10-Sep-18 10:29am    
it compares what I have in my datatable to the lines of the document. What I would like to do is have a way to use the database information and have it fill in the .replace method.

I just dont know how to make the code look like this:
regstr.Replace("text", "text").Replace("text", "text").Replace("text", "text"). 'etc

where it automatically will add the .Replace("text", "text") for every datatable entry that matche.
Ralf Meier 10-Sep-18 14:04pm    
I understand ...
What I suggest is : you make a method (function) to which you give the source-string and also the replacements (for example as a List or an Array).
Inside your method you look for the size of the Array (it's length) and iterate through it and do your replacements item by item.
If you build up a combined command like in your code-sample (replacestr = regstr.Replace("text", "text").Replace("text", "text").Replace("text", "text")) it will also do the replacements step by step (but in this case ordered by the compiler).

1 solution

Here's my take on it.
C#
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
 
Share this answer
 
v3
Comments
Member 11856456 10-Sep-18 15:30pm    
Alek your code enhancements to my code were great, the only issue is that the output into the richtextbox has blank lines. there should be no blank lines in the richtextbox. I tried maneuvering some of the code around, but I am still getting the blank lines.
Alek Massey 10-Sep-18 16:14pm    
Were there blank lines in the output from your code previously?
Member 11856456 10-Sep-18 16:43pm    
Hey Alek, yeah originally when I ran the code the list of items would be on their individual lines with no blank lines. I should have said that in the orignal. I appreciate your approach to the problem.
Alek Massey 10-Sep-18 17:01pm    
I updated my solution. Thanks for the upvote.
Member 11856456 10-Sep-18 17:19pm    
You're welcome, Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900