Question find replace text from file - fastest method ?

jamesvick

Active member
Joined
Jul 19, 2010
Messages
34
Programming Experience
1-3
Alright first my situation :

1) a text document which has content like :

VB.NET:
uber|uber|ultra
taxi|taxi|taxi cab|cab|minicab|airport taxi|airport transfer
genitalia|genitalia|genitals
skincare|skincare|skin care|cosmetic|natual skin care|natural skin care
masses|masses|public|people|world|lots|plenty|wider public|herd|loads|amounts
you can do so|you can do so|that can be done
i wish the|i wish the|the manufacturers should
the reason is that|the reason is that|this is because
big issue|big issue|serious problem|major problem|big problem

this file has 64,000+ lines and all lines are like above. This file has english words and their synonyms.

2) an application with a richtextbox and a button. The user will write some text in the box and when he hits the button, the application will read the whole text and then do this :

* read the text file line by line
* find all the words in the box - the words are chosen by splitting the line from text file at "|". The first word is the word to find. For eg : uber, skincare, big issue etc.
* replace the word by the other part of split string

My code :

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fil2 As New StreamReader("new2.txt")

        Dim i As Integer = 0
        Dim start2 As Integer = 0

        Do While fil2.Peek > -1
            Dim StringToCheck As String = fil2.ReadLine()
            Dim prev As String = ""

            If (StringToCheck.Split("|")(0).Split(" ").Count >= 2) Then
                Try
                    Dim z As Integer = RichTextBox1.Find(StringToCheck.Split("|")(0).Trim(), start2, RichTextBoxFinds.WholeWord)

                    If (z > 0) Then
                        'rtfArticle.SelectionColor = Color.Orange
                        RichTextBox1.Select(z, StringToCheck.Split("|")(0).Trim().Length)

                        Dim ss2 As String = ""
                        Dim ss3 As String = ""

                        If (RichTextBox1.SelectedText <> "") Then
                            
RichTextBox1.SelectedText = StringToCheck.Split("|")(1) & "|" & StringToCheck.Split("|")(2) & "|" & StringToCheck.Split("|")(3) & "|" & StringToCheck.Split("|")(4)
                                
                        End If

                        start2 = 0

                    End If

                Catch ex As Exception

                End Try
            End If
            

        Loop

        fil2.Close()


    End Sub

now the problem is that this takes hell lot of time. I tried with approx. 2500+ lines in richtextbox and it took 50 mins.

My thinking of solution : Iam thinking of running like 25 threads and passing 100 lines to each thread to the operation. But iam sure there should be another method or commercial component to do the same?

Any help plz.
 
Last edited:
Erm.. Yeah. Kinda would take ages if youre using a UI component. Just read the whole file into a StringBuilder and then run your replacements using the Replace() method. Right now I can't even work out why anyone would think that loading a massive file into a UI component and running a find replace on it would be quick..

VB.NET:
Dim sb as New StringBuilder(File.ReadAllText(path))
'repl is a 2D array N long by 2 wide, find in the first element, replacement in the second
For i = 0 To repl.Count -1
  sb.Replace(repl(i)(0), repl(i)(1))
Next i
 
Back
Top