Hi Everyone,
This is a very complicated, if not impossible problem.
I have two strings I want to compare. The problem is that somewhere in
both strings there is a text which is identical, but it's never on the
left or right.
example:
string1 = "THIS ---- MY.NEW.FILE.OF.TODAY."
string2 = "MY.NEW.FILE.OF.TODAY.AND.TOMORROW."
the similarity is of course the part saying:
"MY.NEW.FILE.OF.TODAY."
I need a code getting me this part of the string.
Whooh..... ain't that though.
Marco 8 1955
<vo***********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com... Hi Everyone,
This is a very complicated, if not impossible problem. I have two strings I want to compare. The problem is that somewhere in both strings there is a text which is identical, but it's never on the left or right.
example: string1 = "THIS ---- MY.NEW.FILE.OF.TODAY." string2 = "MY.NEW.FILE.OF.TODAY.AND.TOMORROW."
the similarity is of course the part saying: "MY.NEW.FILE.OF.TODAY."
I need a code getting me this part of the string.
Whooh..... ain't that though.
Marco
That is one similarity. Others are:
MY
NEW
FILE.OF.TODAY
A
N
So are you saying you are only looking for only the longest string which is
both of part of String1 and String2? What happens if String1="cats like
dogs" and String2="dogs like cats"? Would the function return "dogs" or
"cats" or possibly an array containing both? vo***********@gmail.com wrote: Hi Everyone,
This is a very complicated, if not impossible problem. I have two strings I want to compare. The problem is that somewhere in both strings there is a text which is identical, but it's never on the left or right.
example: string1 = "THIS ---- MY.NEW.FILE.OF.TODAY." string2 = "MY.NEW.FILE.OF.TODAY.AND.TOMORROW."
the similarity is of course the part saying: "MY.NEW.FILE.OF.TODAY."
I need a code getting me this part of the string.
Whooh..... ain't that though.
Marco
It would appear you need to do some loops. I might do something like
this... create string3 and string4. String3 would change all characters
not alph or space in string1 to a space. String4, from string2, same
thing.
comp s2 to s4. Same? Then no change, both equal.
No same? Then get into a loop.
First, comp loop would be for the following
"This My New File Of Today"
"This My New File Of"
"This My New File"
"This My New"
"This My"
"This"
Break on any that were found. If not, remove the first word and try again
"My New File Of Today"
"My New File Of"
"My New File"
"My New"
"My"
Break on any that were found. If not, remove the next first word and
try again
"New File Of Today"
"New File Of"
"New File"
"New"
and so on....
You should be able to use the following functions and command
Do While
For Next
Instr()
Mid()
Left()
Right()
The reason I removed anything not alpha is that I wanted spaces to
delimit words. When you have found the word, with
Instr(string4,string3) you will get the position. Then all you need is
to go to that position in string2 for the length of string3 to get the
corresponding value. You'll now know the common phrase between the two.
By going from longest string to shortest string you should get the
longest string between the two.
your problem is stated: http://en.wikipedia.org/wiki/Longest...string_problem
the following function which appears to solve it was snarfed from : http://www.planetsourcecode.com/vb/s...54427&lngWId=1
Public Function LongestCommonSubstring(A As String, B As String) As String
Dim sLenA, sLenB, sLen, i, j, idxA, idxB As Integer
sLenA = Len(A): sLenB = Len(B)
ReDim ArrLCS(0 To sLenA, 0 To sLenB) As Integer
For i = 1 To sLenA
For j = 1 To sLenB
If UCase$(Mid$(A, i, 1)) <> UCase$(Mid$(B, j, 1)) Then
ArrLCS(i, j) = 0
Else
ArrLCS(i, j) = 1 + ArrLCS(i - 1, j - 1)
If ArrLCS(i, j) > sLen Then
sLen = ArrLCS(i, j)
idxA = i: idxB = j
End If
End If
Next
Next
LongestCommonSubstring = Mid$(A, idxA - sLen + 1, ArrLCS(idxA, idxB))
Erase ArrLCS
End Function
"Malcolm Cook" <me*@stowers-institute.org> wrote in message
news:_h***************@news.more.net... your problem is stated: http://en.wikipedia.org/wiki/Longest...string_problem
the following function which appears to solve it was snarfed from : http://www.planetsourcecode.com/vb/s...54427&lngWId=1
Public Function LongestCommonSubstring(A As String, B As String) As String Dim sLenA, sLenB, sLen, i, j, idxA, idxB As Integer
sLenA = Len(A): sLenB = Len(B) ReDim ArrLCS(0 To sLenA, 0 To sLenB) As Integer
For i = 1 To sLenA For j = 1 To sLenB If UCase$(Mid$(A, i, 1)) <> UCase$(Mid$(B, j, 1)) Then ArrLCS(i, j) = 0 Else ArrLCS(i, j) = 1 + ArrLCS(i - 1, j - 1) If ArrLCS(i, j) > sLen Then sLen = ArrLCS(i, j) idxA = i: idxB = j End If End If Next Next
LongestCommonSubstring = Mid$(A, idxA - sLen + 1, ArrLCS(idxA, idxB)) Erase ArrLCS End Function
That's a pretty compact solution and nicely answers my question what happens
if String1="cats like dogs" and String2="dogs like cats" - would the
function return "dogs" or "cats"?
Answer: neither, you idiot. It returns "s like "
Fantastic code.
Just changed the Strings in Variants so you can use a sStrA and sStrB
instead of already having to fillin a string.
Marco
On Fri, 7 Oct 2005 20:05:55 +0000 (UTC), "Brian Wilson"
<bw*****@ease.co.uk> wrote:
See: cats don't like dogs.
-Tom. "Malcolm Cook" <me*@stowers-institute.org> wrote in message news:_h***************@news.more.net... your problem is stated: http://en.wikipedia.org/wiki/Longest...string_problem
the following function which appears to solve it was snarfed from : http://www.planetsourcecode.com/vb/s...54427&lngWId=1
Public Function LongestCommonSubstring(A As String, B As String) As String Dim sLenA, sLenB, sLen, i, j, idxA, idxB As Integer
sLenA = Len(A): sLenB = Len(B) ReDim ArrLCS(0 To sLenA, 0 To sLenB) As Integer
For i = 1 To sLenA For j = 1 To sLenB If UCase$(Mid$(A, i, 1)) <> UCase$(Mid$(B, j, 1)) Then ArrLCS(i, j) = 0 Else ArrLCS(i, j) = 1 + ArrLCS(i - 1, j - 1) If ArrLCS(i, j) > sLen Then sLen = ArrLCS(i, j) idxA = i: idxB = j End If End If Next Next
LongestCommonSubstring = Mid$(A, idxA - sLen + 1, ArrLCS(idxA, idxB)) Erase ArrLCS End Function
That's a pretty compact solution and nicely answers my question what happens if String1="cats like dogs" and String2="dogs like cats" - would the function return "dogs" or "cats"? Answer: neither, you idiot. It returns "s like "
It would return "like"
"Brian Wilson" <bw*****@ease.co.uk> wrote in message
news:di**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com... <vo***********@gmail.com> wrote in message news:11*********************@g47g2000cwa.googlegro ups.com... Hi Everyone,
This is a very complicated, if not impossible problem. I have two strings I want to compare. The problem is that somewhere in both strings there is a text which is identical, but it's never on the left or right.
example: string1 = "THIS ---- MY.NEW.FILE.OF.TODAY." string2 = "MY.NEW.FILE.OF.TODAY.AND.TOMORROW."
the similarity is of course the part saying: "MY.NEW.FILE.OF.TODAY."
I need a code getting me this part of the string.
Whooh..... ain't that though.
Marco That is one similarity. Others are: MY NEW FILE.OF.TODAY A N So are you saying you are only looking for only the longest string which is both of part of String1 and String2? What happens if String1="cats like dogs" and String2="dogs like cats"? Would the function return "dogs" or "cats" or possibly an array containing both?
"Bruce Rusk" <br***************@stanford.edoo> wrote in message
news:di**********@news.Stanford.EDU... It would return "like"
As I pointed out earlier, it acually returns "s like " This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gordon Airport |
last post by:
Has anyone suggested introducing a mutable string type (yes, of course)
and distinguishing them from standard strings by the quote type - single
or double? As far as I know ' and " are currently...
|
by: J.W. |
last post by:
How do I compare strings in javascript? The "==" double equals or
"!=" doesn't seem to work in this case.
I'm sure string comparison has been explained before but searching
Google didn't find...
|
by: Drew |
last post by:
Hello -
I am a converted VB programmer. What I am trying to do
it compare two strings in an if statement. The problem
is that when I use string.compare it always returns a
negative 1. I have...
|
by: David zhu |
last post by:
I've got different result when comparing two strings
using "==" and string.Compare().
The two strings seems to have same value "1202002" in
the quick watch, and both have the same length 7 which I...
|
by: balakrishnan.dinesh |
last post by:
hi frnds,
Im having two 20digit numbers, But while comparing those it
is giiving wrong ouput in javascript.
for example here is my code,
my secanrio is ,
~ If first 20 digit number is...
|
by: Twinkle |
last post by:
HI there
i want to compare between two strings char by char.every
strings having a word document.first string name is strFileName and
second string name is strFilename1.
i want to compare...
|
by: titan nyquist |
last post by:
I wish to compare two structs via == but it does not compile. I can
overload and create my own == but am I missing something that c#
already has implemented?
~titan
|
by: Peter Proost |
last post by:
Hi group, I want to compare path strings in order to sort them, assuming I
have got:
"a.txt"
"dir1\c.txt"
"e.txt"
"dir1\d.txt"
When I compare them using "e.text" would be greater than...
|
by: Tony |
last post by:
Hello!
Below I have two different ways to test if the instance tb.Text contains the
string "Programmer"
So which one to use is it just a matter of taste ?
Or could it be some advantage to one...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |