• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Comparing two strings

Okay, I have looked and looked and can't find what I am looking for.  I have a program that compares a webpage every week.  It strips down the tags and gets the barebone text of the page and should compare it to the previous week.  Before I had it compare each letter from the start until something change and then went in reverse until it changed and showed me that block of text in red.  Problem was it would not give me true results.  If one word was change at the beginning and one and the end it would have a huge block of text when it might be just two words.  I am looking for something that would just return those two words in there location and nothing else.

String1 = "I am looking to compare two text files and return the results."
String2 = "I am trying to compare text files and return the results."
Results = "looking two"
Before it would return: "looking to compare t"

Obviously totally different.  Let me know if you have any questions.  Any help to get on the right track would be appreciated.  Thanks.
0
tw_chase
Asked:
tw_chase
1 Solution
 
rdrunnerCommented:
What language are you using?

Might be usefull to know...

Suggestion... How about creating to arrays that hold the words and then doing a word comparrison?
0
 
tw_chaseAuthor Commented:
This is all done in Visual Basic 6.  Sorry about that.  I thought I was in that area.
0
 
rdrunnerCommented:
What do you think about the option to create 2 arrays and compare them word by word?

The question here is how you would want to compare these 2 strings...

"buy our cheap stuff"

and

"buy our really really really really cheap stuff"

or vice versa...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mikal613Commented:
Private Sub Command1_Click()
  Msgbox Difference("MyString","mystring")
End Sub

Private Function Difference(ByVal String1 As String,ByVal String2 As String) As String
  Dim strMessage As String
  Dim intPos As Integer
  If Len(String1) <> Len(String2) Then
    Difference = "The strings are of different lengths"
    Exit Function
  End If
  For intPos = 1 To Len(String1)
    If Mid(String1,intPos,1) <> Mid(String2,intPos,1) Then
      strMessage = strMessage & "Position " & CStr(intPos) & " : " & Mid(String1,intPos,1) & " <> " & Mid(String2,intPos,1) & vbLF
    End If
  Next
  Difference = strMessage
End Function
0
 
tw_chaseAuthor Commented:
All that does is tells me the strings are different lengths.  Thanks for the reply.

Todd
0
 
rdrunnerCommented:
Please post the desired results for the 2 example strings i gave you ....

But include both ways... If i compare the 1st vs the 2nd and vice versa....

"buy our cheap stuff"

and

"buy our really really really really cheap stuff"
0
 
tw_chaseAuthor Commented:
"really really really really" Would be the results.
0
 
abdullah716Commented:
Ok, here goes. Create a new Windows Form in VB.Net and paste this code into it:

'  == Begin code ==
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.TextBox3 = New System.Windows.Forms.TextBox
        Me.TextBox4 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(16, 32)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TextBox1.Size = New System.Drawing.Size(272, 120)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "Free and easy to use"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(296, 32)
        Me.TextBox2.Multiline = True
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TextBox2.Size = New System.Drawing.Size(272, 120)
        Me.TextBox2.TabIndex = 1
        Me.TextBox2.Text = "Free and very very very easy to work with"
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(16, 160)
        Me.TextBox3.Multiline = True
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TextBox3.Size = New System.Drawing.Size(272, 120)
        Me.TextBox3.TabIndex = 2
        Me.TextBox3.Text = ""
        '
        'TextBox4
        '
        Me.TextBox4.Location = New System.Drawing.Point(296, 160)
        Me.TextBox4.Multiline = True
        Me.TextBox4.Name = "TextBox4"
        Me.TextBox4.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TextBox4.Size = New System.Drawing.Size(272, 120)
        Me.TextBox4.TabIndex = 3
        Me.TextBox4.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(488, 288)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 4
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(584, 317)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox4)
        Me.Controls.Add(Me.TextBox3)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox3.Text = ""
        TextBox4.Text = ""

        Dim Array1, Array2 As String()
        array1 = TextBox1.Text.Split(" ")
        array2 = TextBox2.Text.Split(" ")

        Dim WordMatches1 As New ArrayList
        Dim WordMatches2 As New ArrayList

        Dim i As Integer
        For i = 0 To Array1.Length - 1
            Dim wm As New WordMatch(Array1(i))
            WordMatches1.Add(wm)
        Next
        For i = 0 To Array2.Length - 1
            Dim wm As New WordMatch(Array2(i))
            WordMatches2.Add(wm)
        Next

        i = 0
        For Each wm As WordMatch In WordMatches1
            wm.IsMatched = WordIsMatched(wm.Word, Array2, i)
            If wm.IsMatched Then
                i += 1
            End If
        Next

        i = 0
        For Each wm As WordMatch In WordMatches2
            wm.IsMatched = WordIsMatched(wm.Word, Array1, i)
            If wm.IsMatched Then
                i += 1
            End If

        Next

        ' Fill textbox and show results
        For Each wm As WordMatch In WordMatches1
            If wm.IsMatched Then
                TextBox3.Text &= wm.Word
            Else
                TextBox3.Text &= "[" & wm.Word & "]"
            End If
            TextBox3.Text &= " "
        Next

        For Each wm As WordMatch In WordMatches2
            If wm.IsMatched Then
                TextBox4.Text &= wm.Word
            Else
                TextBox4.Text &= "[" & wm.Word & "]"
            End If
            TextBox4.Text &= " "
        Next
    End Sub

    Private Function WordIsMatched(ByVal strWord As String, ByVal arrSearchArray As String(), ByVal intStartIndex As Integer) As Boolean
        Dim i As Integer
        If intStartIndex >= arrSearchArray.Length Then
            Return False
        Else
            For i = intStartIndex To arrSearchArray.Length - 1
                If arrSearchArray(i) = strWord Then
                    Return True
                End If
            Next
            Return False
        End If
    End Function

    Private Class WordMatch
        Public Word As String = ""
        Public IsMatched As Boolean
        Public Sub New(ByVal strWord As String)
            Me.Word = strWord
            Me.IsMatched = False
        End Sub
    End Class
End Class
' == End code ==


It will show compare the text in box1 and box2 and outputs the results in box3 and box4.

The changed words will be enclosed between brackets.

[Hey, I had fun doing this :). Nice question, indeed.]
0
 
tw_chaseAuthor Commented:
I am using VB6 and not VB.Net.  How I worked around it was I used an array to collect the old info string in three word sets.  In then uses the InStr function to get and highlight the matching sets of words in the new string that I added to a RichTextBox.  Once it is do scanning there should only be the new words not highlighted.  I then do another InStr function on the RichTextBox looking for the "\cf1" and "\cf0" tags that states were the non-highlighted text is located.  I will accept the VB.Net answer even though I can't use it.  Thanks for everybodies help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now