D
David Turner
The problem seems relatively straightward to the human eye but I imagine it's
quite difficult to do in VBA without prior knowledge of what the strings
contain.
If I have two strings like:
The quick red fox jumped over the lazy brown cow
The quick red fox just stood there
How would I compare them and identify "quick red fox" as the longest common
sub-string?
I suppose I would have to start by reading one of the strings into an array
and comparing its elements against the second string? Or somehow use the
Filter function to send the matching items to another array? But then I can
only seem to be able compare each element against one word ("quick", "red" or
"fox") which I would have to know in advance.
There's no doubt some better way.
Any advice greatly appreciated.
Sub CompareStrings()
Dim Array1() As String
Dim string1 As String
Dim string2 As String
Dim InString() As String
Dim i As Integer
Dim j As Integer
string1 = "The quick red fox jumped over the lazy brown cow"
string2 = "The quick red fox just stood there"
For i = 0 To UBound(Split(string1, " ")) - 1
ReDim Preserve Array1(i)
Array1(i) = Split(string1, " ")(i)
'MsgBox Array1(i)
Next
InString = Filter(Array1, "red", True)
For j = 0 To UBound(InString)
MsgBox InString(j)
Next j
End Sub
quite difficult to do in VBA without prior knowledge of what the strings
contain.
If I have two strings like:
The quick red fox jumped over the lazy brown cow
The quick red fox just stood there
How would I compare them and identify "quick red fox" as the longest common
sub-string?
I suppose I would have to start by reading one of the strings into an array
and comparing its elements against the second string? Or somehow use the
Filter function to send the matching items to another array? But then I can
only seem to be able compare each element against one word ("quick", "red" or
"fox") which I would have to know in advance.
There's no doubt some better way.
Any advice greatly appreciated.
Sub CompareStrings()
Dim Array1() As String
Dim string1 As String
Dim string2 As String
Dim InString() As String
Dim i As Integer
Dim j As Integer
string1 = "The quick red fox jumped over the lazy brown cow"
string2 = "The quick red fox just stood there"
For i = 0 To UBound(Split(string1, " ")) - 1
ReDim Preserve Array1(i)
Array1(i) = Split(string1, " ")(i)
'MsgBox Array1(i)
Next
InString = Filter(Array1, "red", True)
For j = 0 To UBound(InString)
MsgBox InString(j)
Next j
End Sub