ben ben - 1 year ago 66 Question

How to find and delete matching words in two different text files in VB?

Its a wordy question, but basically I want to have VB check

for matching words in
, and then delete all instances of similar words in
and output the results as

Outputting results shouldn't be too difficult, but i'm unsure on where to proceed when it comes to recognizing similar wordage. Also, would it be possible to set up a whitelist (there's going to be one word repeated over and over - which I don't want deleted).

This is my open file / read file dialogue which I use for both prompts, and both are displayed visually in a textbox.

Sub Maplist()
If txtFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim sr As New System.IO.StreamReader(txtFile.FileName)
tbx1.Text = sr.ReadToEnd
End If

Thanks for any help!

A sample of the first text file looks like this:

map_01, 200/250

map_03, 358/450

map_06, 528/2000

The second file looks like:







Basically the second file is the "master list'. I want the program to recognize a matching word between both files (for instance, the 01 in map_01) and then delete the entry from the master list. When I was talking about whitelisting I was concerned that it would match a word like"map" and delete everything in the master list.

Answer Source

You have to make an array of words from both the TextFile and compare those. I did it for you here:

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    'make array of words from both the files
    Dim text1Array() As String = TextBox1.Text.Split(" ")
    Dim text2Array() As String = TextBox2.Text.Split(" ")
    Dim NewText As String
    NewText = TextBox2.Text
    'loop through all the words in first array
    For i = 0 To text1Array.Length - 1
        'loop through all the words in second array
        For j = 0 To text2Array.Length - 1
            'match the words
            If text1Array(i) = text2Array(j) Then
                'replace the found word with an empty character
                NewText = NewText.Replace(text2Array(j), String.Empty)
                'delete double space 
                NewText = NewText.Replace("  ", " ")
            End If
    'save it into third textbox
    TextBox3.Text = NewText
End Sub

I checked it like this:

Textbox1 contained : 

one two three four five six seven eight nine ten eleven twelve

TextBox2 contained :

one subaz three sarma five loves six coding eight ten all the twelve time

After clicking the button, TextBox3 contained:

subaz sarma loves coding all the time

This works perfectly fine.

Now, about the whitelist, let's say you don't want to remove "five" even if it's matched. Do this:

Dim WhiteListWord As String = "five"

And change the condition as :

 If text1Array(i) = text2Array(j) And text1Array(i) <> WhiteListWord Then

New output in the textbox3 will be :

subaz sarma five loves coding all the time

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download