-
June 3rd, 2011, 07:37 AM
#1
[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
-
June 3rd, 2011, 07:58 AM
#2
Re: comparing two strings
You could use the StrComp function
-
June 3rd, 2011, 08:02 AM
#3
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.
-
June 3rd, 2011, 08:44 AM
#4
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
-
June 3rd, 2011, 08:47 AM
#5
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.
-
June 3rd, 2011, 08:48 AM
#6
Re: comparing two strings
Did you at least attempt to google strComp????? Seriously dude get with the program!
//DM - you were faster this time
-
June 3rd, 2011, 08:52 AM
#7
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.
-
June 3rd, 2011, 09:05 AM
#8
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.
-
June 3rd, 2011, 11:41 AM
#9
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
-
June 3rd, 2011, 12:20 PM
#10
Re: comparing two strings
my problem is how can i output the differnce string by using DM's code.
-
June 3rd, 2011, 12:28 PM
#11
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.
-
June 3rd, 2011, 12:37 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|