CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2011
    Posts
    34

    [RESOLVED] comparing two strings

    how can i compare two strings then it will output me the difference,, i cant figure out how to make it i googled it and cant findy any answer for my question..

    its like i have two strings
    x = "abc"
    y = "abcd"

    then what im trying to do is im going to compare them,,then give me an output of "d" which is the difference of the two stings..


    im stuck with this kind of code in my mind i dont know how to make it.
    Code:
    Private Sub Form_Load()
    Dim x As String
    Dim y As String
    
    x = "aa"
    y = "aab"
    
    
    
    
    If x = y Then
    label1.Caption = "equal"
    
    Else
    label1.Caption = "not equal"
    End If
    
    
    End Sub

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: comparing two strings

    You could use the StrComp function

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: comparing two strings

    You could loop through the strings and compare each character 1 at a time.

    In your else block above something like
    Code:
    For I = 1 to len(x)
        If mid(x,I,1)<> Mid(y,I,1) then
            'character in this postion does not match
        end if
    Next
    Edit: or the strComp function
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Mar 2011
    Posts
    34

    Re: comparing two strings

    how will it output the diffrnce in the string? i mean i would like to output "d" which is the difference of string x and y .

    and how to use the strComp?

    thanks for the help

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: comparing two strings

    I would suggest that you look up strcomp in your online help or google it.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: comparing two strings

    Did you at least attempt to google strComp????? Seriously dude get with the program!

    //DM - you were faster this time

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: comparing two strings

    btw the loop example I posted assumes both strings have the same number of characters. More code is needed for strings that are not the same length.

    The loop method will allow you full control over the test and the output.
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Mar 2011
    Posts
    34

    Re: comparing two strings

    ive tried this one but then i dont know how can i output its difference..
    sorry guys new to vb.

    Code:
    Private Sub Form_Load()
    Dim x As String
    Dim y As String
    
    x = "aaa"
    y = "aab"
    
    
    If StrComp(x, y, vbTextCompare) = 0 Then
    MsgBox "equal"
    Else
    MsgBox "notequal"
    End If
    Last edited by jelopy15; June 3rd, 2011 at 09:08 AM.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: comparing two strings

    If the code is NOT EQUAL, then calculate the LENGTH of each string.

    Then, use this code, that DM wrote:
    Code:
    For I = 1 to len(x) '    Use LengthOfA or LengthOfB instead of Len(x)
        If mid(x,I,1)<> Mid(y,I,1) then
            'character in this postion does not match  '  Print the character using ;
                                                                               ' or build a new string
        end if
    ' Print the differences string, or else a BLANK LINE to finish the words.
    Next
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Join Date
    Mar 2011
    Posts
    34

    Re: comparing two strings

    my problem is how can i output the differnce string by using DM's code.

  11. #11
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: comparing two strings

    Within the if section you would add code to output the characters as you wish
    Example:
    Code:
    If mid(x,I,1)<> Mid(y,I,1) then
            'character in this postion does not match 
            Label1.caption=label1.caption & mid(y,I,1)                                                                           
        end if
    Again the above is a simple code which only handles strings that are the same lenght, you will need additional code to handle strings of unequal lenght.

    Everything you need is there, Len() For Next Loop MID()

    You will learn a lot more if you play with it a bit and try to figure it out from there.
    Last edited by DataMiser; June 3rd, 2011 at 12:30 PM.
    Always use [code][/code] tags when posting code.

  12. #12
    Join Date
    Mar 2011
    Posts
    34

    Re: comparing two strings

    thanks for all the help guys,,i got it already..thanks you so much..problem solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured