1

I am newbie in programming and would like to ask if I could separate the string below. I am using Visual Basic. Basically I have two string below :

String 1 : gOldStr= TEDDYBEARBLACKCAT

String 2 = gNewStr= BLACKCATWHITECAT

I wanted to separated the string 2 by looking the exact value in String 1

so that I have String2 that is part of string 1 = BLACKCAT

String 2 that is new = WHITECAT

I have tried below script but it doesn't work all the time. Could suggest me the better logic? Thanks2

For i=1 to Len(gOldStr)
        TempStr = Left$(gNewStr,i) 
        Ctr1 = InStr(gOldStr, TempStr)
        gTemporary = Mid$(gOldStr,Ctr1)

        gTemporary = Trim(gTemporary)

        Ctr2 = StrComp(gOldStr, gTemporary)
        If Ctr2=1 Then
                gTemporary2 = Replace(gNewStr,gTemporary,"")
                Exit For
        End If
Next i 
Nopio
  • 13
  • 3
  • 1
    Welcome to Stackoverflow. Using `InStr` will search "TEDDYBEARBLACKCAT" for "BLACKCATWHITECAT". You will need to separate the words in String1 and then put the words into an `Array` or have separate strings for each search term. – Jean-Pierre Oosthuizen Apr 29 '16 at 08:24
  • Hi Jean, Thank you for ur reply. Actually when I tried to do Ctr1 = InStr(gOldStr, gNewStr) , it will return me with 0 position. Though I also can't separate the words manually as both data is inputted by user. – Nopio Apr 29 '16 at 10:04
  • @nopio It gives you 0, because InStr checks if the whole of gNewStr is within gOldStr, which it is not because they are not entirely the same. See my answer below though... – ib11 Apr 29 '16 at 11:05

1 Answers1

0

If the common part is always at the end of the first one and at the beginning of the 2nd one, you could look from the end, like this:

Dim strMatchedWord As String
For i=1 to Len(gOldStr)
    If i>Len(gNewStr) Then Exit For  'We need this to avoid an error
    Test1 = Right$(gOldStr, i)
    Test2 = Left$(gNewStr, i)

    If Test1 = Test2 Then
        strMatchedWord = Test1     'Store your match is Test1
    End If
Next
Debug.Print strMatchedWord 'Once the loop finishes it contains the longest match 

I modified the code so that the loop does not exit until it went through the full string. This way it will get you the longest match by the end of the loop.

ib11
  • 2,530
  • 3
  • 22
  • 55
  • Hi @ib11 thanks for ur input, yeah thats works too. but i am thinking if the string has repetitive character, this method also won't get the correct string. Example : String 1 : AAADEFDEF String 2: DEFDEFJJJWWW – Nopio Apr 30 '16 at 11:06
  • Okay. I updated the code. Now it goes through the entire string and if it finds a match it stores it, so by the end you have the longest one. – ib11 Apr 30 '16 at 23:38
  • hi @ib11 thank you, it works perfectly. Thanks a lot. – Nopio May 03 '16 at 01:43
  • Excellent. Please don't forget to upvote and accept the answer: [What should I do when someone answers my question?](http://meta.stackoverflow.com/help/someone-answers) – ib11 May 03 '16 at 02:05