Results 1 to 3 of 3

Thread: Finding a string between two words

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Posts
    183

    Question Finding a string between two words

    Hello all,

    I would like to be able to find out what is in the textbox between the words QQQ1 and QQQ2, however I am not sure how to do this. The code I tried to do it with is...
    VB Code:
    1. Public Sub TrimString(TheString As String, KeepAfter As String, KeepBefore As String)
    2. Dim posA As Long
    3. Dim posB As Long
    4. posA = 1
    5. posB = 1
    6. Dim incTill As Long
    7. incTill = Len(TheString) - Len(KeepAfter)
    8. Debug.Print incTill & " is intill"
    9. For i = 1 To incTill
    10. If Mid(TheString, posA, Len(KeepAfter)) = KeepAfter Then
    11. Debug.Print posA
    12. GoTo FoundKeepAfter
    13. posA = i
    14. End If
    15. Next
    16. TheString = "Cannot find KeepAfter"
    17. Exit Sub
    18. FoundKeepAfter:
    19. incTill = Len(TheString) - Len(KeepBefore)
    20. For j = 1 To incTill
    21. If Mid(TheString, posB, Len(KeepBefore)) = KeepBefore Then
    22. GoTo FoundKeepBefore
    23. posB = j
    24. End If
    25. Next
    26. TheString = "Cannot find KeepBefore"
    27. Exit Sub
    28. FoundKeepBefore:
    29. TheString = Mid(TheString, posA, posA - posB)
    30. DoEvents
    31. Text1.Text = TheString
    32. End Sub
    33.  
    34. Private Sub Command1_Click()
    35. TrimString Text1.Text, "QQ1", "QQQ2"
    36. End Sub
    But it doesnt work Is there some good way to do it?

    Thanks

  2. #2
    Hyperactive Member tpfkanep's Avatar
    Join Date
    Jun 2002
    Location
    South Africa
    Posts
    272
    VB Code:
    1. Dim strSearch1 As String
    2. Dim strSearch2 As String
    3. Dim Q1Pos As Integer
    4. Dim Q2Pos As Integer
    5. Dim strMid As String
    6.  
    7. strSearch1 = "This is blahQQQ1You cannot find meQQQ2"
    8.  
    9. Q1Pos = InStr(1, strSearch1, "QQQ1")
    10. Q2Pos = InStr(1, strSearch1, "QQQ2")
    11.  
    12. strMid = Mid(strSearch1, Q1Pos + 4, Q2Pos - Q1Pos - 4)
    13.  
    14. MsgBox strMid
    15.  
    16. strSearch2 = "QQQ1You cannot find meQQQ2"
    17. Q1Pos = InStr(1, strSearch2, "QQQ1")
    18. Q2Pos = InStr(1, strSearch2, "QQQ2")
    19.  
    20. strMid = Mid(strSearch2, Q1Pos + 4, Q2Pos - Q1Pos - 4)
    21.  
    22. MsgBox strMid

    PS: I did not run thru your code, BUT your are invoking the
    TrimString Text1.Text, "QQ1", "QQQ2" routine. Should the 1st parameter not be QQQ1"?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Posts
    183

    thanks

    THANKS! That works great for me, and I also understand it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width