Prev: coverting iWord.pages file
Next: Word.ActiveDocument.AttachedTemplate.Saved = True - gives "Type Mismatch" error
From: David Turner on 3 Feb 2010 09:04 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
From: Karl E. Peterson on 3 Feb 2010 13:39 David Turner wrote: > 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? Perhaps it's not even very easy to the human eye? (The longest common substring was actually "The quick red fox j", not "quick red fox".) > 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. I'd suggest you start by clearly enumerating your actual requirements. Is this a character comparison or a word comparison? Do the matches need to start at the same position, or may they be anywhere within the string. Etc, etc, etc... If you can't define the problem, you'll never solve it. -- ..NET: It's About Trust! http://vfred.mvps.org
From: Neil Humphries on 3 Feb 2010 13:48 I would put them in a For Next loop incremented from 1 to the length of the shortest string. Use the Mid() function to extract the characters in the shortest string one by one starting at the first. Use the Instr() function to search for the character in the longer string. As long as the strings are a match, increment and keep looping. When they don't match, use the index of the loop to tell you the position. Use the Left() function to return the portion of the string that matches. This will only work if the two string start out the same. It gets more complex if the patterns match in the middle. "David Turner" wrote: > 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
From: Fumei2 via OfficeKB.com on 3 Feb 2010 14:06 I would agree with Karl. As, technically, the largest sub-string is indeed "The quick red fox j", you need to spell out EXACTLY the requirements. Neil: "This will only work if the two string start out the same. It gets more complex if the patterns match in the middle." That is putting it mildly. This is a very complex task. Quite likely do-able, but precise requirements are needed. Neil Humphries wrote: >I would put them in a For Next loop incremented from 1 to the length of the >shortest string. >Use the Mid() function to extract the characters in the shortest string one >by one starting at the first. >Use the Instr() function to search for the character in the longer string. >As long as the strings are a match, increment and keep looping. When they >don't match, use the index of the loop to tell you the position. >Use the Left() function to return the portion of the string that matches. > >This will only work if the two string start out the same. It gets more >complex if the patterns match in the middle. > >> 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 >[quoted text clipped - 36 lines] >> >> End Sub -- Message posted via OfficeKB.com http://www.officekb.com/Uwe/Forums.aspx/word-programming/201002/1
From: David Turner on 3 Feb 2010 15:59 "Karl E. Peterson" wrote: > I'd suggest you start by clearly enumerating your actual requirements. > Is this a character comparison or a word comparison? Do the matches > need to start at the same position, or may they be anywhere within the > string. Etc, etc, etc... > > If you can't define the problem, you'll never solve it. Sorry. Word comparison, the matches can start anywhere in the strings and the strings could be of variable length, possibly ending in punctuation: two sentences for example. I now realise it's a much more complex problem than I first thought.
|
Next
|
Last
Pages: 1 2 3 Prev: coverting iWord.pages file Next: Word.ActiveDocument.AttachedTemplate.Saved = True - gives "Type Mismatch" error |