|
-
Aug 6th, 2003, 06:49 AM
#1
Thread Starter
Hyperactive Member
Compare two strings and get the differance
Hi,
I have 2 strings
string1 = "abcdefgh"
string2 = "abcdefghijklm"
I want to get the difference of the two strings .....
like getDiff(str2,str1) = "ijklm"
and where the diffrance is
like
getDiffStart(str2,str1) = 8 (where the last commom charcater is)
...........
Please help
It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
-Aristotle As quoted in Rapid Development, chapter 8, page 167.
-
Aug 6th, 2003, 07:08 AM
#2
PowerPoster
use the mid$ function in a loop to grab one char at a time from each string and compare them
-
Aug 6th, 2003, 07:14 AM
#3
you mean something like this? :
VB Code:
[color=blue]Private Sub[/color] Command1_Click()
MsgBox GetDifference("abcdefgh", "abcdefghijklm")
[color=blue]End Sub[/color]
[color=blue]Private Function[/color] GetDifference([color=blue]ByVal[/color] stringA [color=blue]As String[/color], [color=blue]ByVal[/color] stringB [color=blue]As String[/color]) [color=blue]As String[/color]
GetDifference = [color=blue]Replace[/color](stringB, stringA, "")
[color=blue]End Function[/color]
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Aug 6th, 2003, 07:19 AM
#4
Frenzied Member
VB Code:
Private Sub Form_Load()
Debug.Print GetDiff("Hello", "Hellohihow")
Debug.Print getDiffStart("Hello", "Hellohihow")
End Sub
Public Function GetDiff(S1 As String, S2 As String) As String
If Len(S1) > Len(S2) Then
GetDiff = Replace(S1, S2, "")
Else
GetDiff = Replace(S2, S1, "")
End If
End Function
Public Function getDiffStart(S1 As String, S2 As String) As Integer
Dim Res As String
Res = GetDiff(S1, S2)
If Len(Res) Then
If Len(S1) > Len(S2) Then
getDiffStart = InStr(1, S1, Res)
Else
getDiffStart = InStr(1, S2, Res)
End If
End If
End Function
-
Aug 7th, 2003, 04:16 AM
#5
Thread Starter
Hyperactive Member
how abt .........
What if the two strings are
s1 = "abcdefgh"
s2 = "abgh"
I need to get the difference which is "cde" and where it starts .....
I have richtext box called rtf1
and when the user presses my "Undo" or "Redo" button I want to highlite the difference .....
like in worpad when you delete something and press CTRL + Z the part you delected get's highlited ........
I currently use something like this .........
Code:
' The Redo Part .......... Very Messy Coding
'On Error Resume Next
ignore = True
LockWindowUpdate Me.hwnd
If Not undo(undocount + 1) = "" Then
SelStart = rtfText.SelStart
sellen = rtfText.SelLength
undocount = undocount + 1
RichText1.TextRTF = undo(undocount)
If Len(RichText1.Text) > Len(rtfText.Text) Then
If Left(RichText1.Text, Len(rtfText.Text)) = rtfText.Text Then
SelStart = Len(rtfText.Text)
sellen1 = Abs(Len(RichText1.Text) - Len(rtfText.Text))
End If
End If
rtfText.TextRTF = undo(undocount) ' load the next text
Timer5.Enabled = False
If Not sellen1 > 0 Then
rtfText.SelStart = SelStart + sellen
Else
rtfText.SelStart = SelStart ' + sellen
rtfText.SelLength = sellen1
End If
RichText1.TextRTF = ""
undolen = Len(rtfText.TextRTF)
Call rtfText_SelChange
End If
LockWindowUpdate 0
ignore = False
It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
-Aristotle As quoted in Rapid Development, chapter 8, page 167.
-
Aug 7th, 2003, 04:35 AM
#6
PowerPoster
What if the two strings are
s1 = "abcdefgh"
s2 = "abgh"
I need to get the difference which is "cde" and where it starts .....
use the mid$ function in a loop to grab one char at a time from each string and compare them
-
Aug 8th, 2003, 11:39 AM
#7
Thread Starter
Hyperactive Member
Too complicated
Originally posted by phinds
use the mid$ function in a loop to grab one char at a time from each string and compare them
Well ........ it does not work correctly in strings which contain vbcrlf ..... (I tried)
The Replace and Instr Combinations work on spesific string stypes i encountered .....
for eg. s1 = hello'hi'hello'hi'hello'hi'hello'hi'
s2 = hello'hi'hello''hello'hi'hello'hi'
U c.......... what i mean ......... when middle 'hi' is deleted and we use the
instr function to get the start of the difference .......
Or use replace here ......... it won't work
........... Please help if you guys nkow anything I could use to highlight the diffrence in RTFTEXT box when the user deletes some text and presses Ctrl + Z
It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
-Aristotle As quoted in Rapid Development, chapter 8, page 167.
-
Aug 8th, 2003, 02:53 PM
#8
PowerPoster
Well ........ it does not work correctly in strings which contain vbcrlf ..... (I tried)
don't know what you tried, but the mid$ function doesn't care about vbCrLf and this technique WILL work on any pair of strings. If you have code that "doesn't work", post it.
-
Aug 10th, 2003, 01:46 AM
#9
Thread Starter
Hyperactive Member
Try this
Originally posted by phinds
don't know what you tried, but the mid$ function doesn't care about vbCrLf and this technique WILL work on any pair of strings. If you have code that "doesn't work", post it.
Code:
Function getdiffstart(str1 as string,str2 as string) as integer
for i = 0 to len(str2) - 1
if mid(str2,i,1) <> mid(str1,i,1) then
getdiffstart = i
exit for
end if
next i
End function
' the way to get the actual difference is more complex
It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
-Aristotle As quoted in Rapid Development, chapter 8, page 167.
-
Aug 10th, 2003, 02:36 AM
#10
Fanatic Member
Can you uh... perhaps shorten your sig a bit? You got to admit, it is blooming huge!
-
Aug 10th, 2003, 03:06 AM
#11
Thread Starter
Hyperactive Member
Hi
Originally posted by LITHIA
"Can you uh... perhaps shorten your sig a bit? You got to admit, it is blooming huge!"
Said the desert to the grain of sand ................ lol
It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
-Aristotle As quoted in Rapid Development, chapter 8, page 167.
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
|