Results 1 to 1 of 1

Thread: Find a string between two string points!

  1. #1

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Find a string between two string points!

    This type of function has probably been coded and posted several times, I'm not sure. But I wrote it anyway and I added a little extra bit into it that might interest some folks, especially if you're just getting started in VB ;D

    This function will find the string data between 2 other strings:

    VB Code:
    1. Public Function strFindBetween(strToSearch As String, strStartPoint As String, strEndPoint As String, Optional skipInstance As Integer = 0) As String
    2.  
    3.     'Quit the function if infomation provided doesn't fit requirements
    4.     If strToSearch = "" Or strStartPoint = "" Or strEndPoint = "" Then Exit Function
    5.     If Not InStrB(strToSearch, strStartPoint) > 0 And InStrB(strToSearch, strEndPoint) > 0 Then Exit Function
    6.    
    7.         Dim trimStrLine As String
    8.         Dim strResult As String
    9.         Dim skipInst As Integer
    10.        
    11.         trimStrLine = strToSearch
    12.        
    13.             'Skip instances of first string; default doesn't skip
    14.             For skipInst = 0 To skipInstance
    15.                 trimStrLine = Mid$(trimStrLine, InStr(trimStrLine, strStartPoint) + Len(strStartPoint))
    16.             Next skipInst
    17.        
    18.         'Trim the line to the end of the second specified string
    19.         trimStrLine = Mid$(trimStrLine, 1, InStr(trimStrLine, strEndPoint) - 1)
    20.        
    21.         'Assign the string between the two given string-points
    22.         strFindBetween = trimStrLine
    23.        
    24. End Function


    For example, if you had the string:

    "Hi, my name is Brendan Davis" and you set strStartPoint to "my " and the strEndPoint to " Davis", the function would return the string between those two words(ie. "name is Brendan").

    The kicker is that you can skip the starting point string however many times you wish. Using the skipInstance, if you had the string:

    "Hi! My name is Brendan. My last name is Davis."

    ...and you set the strStartPoint to "My " and the strEndPoint to " Davis", and you set the skipInstance to 1(it defaults at 0), then it would skip over the phrase "My " the first time and go to the next instance of that phrase. Thus, the result in this case would yield:

    "last name is"







    Here's an example of how to call the function:


    VB Code:
    1. Dim strSrch as String
    2.  
    3. strSrch = strFindBetween("Hello, my name is Brendan", "my ", "Brendan")
    4.  
    5. 'This will cause strSrch to equal "name is"

    ..and how to call the function to skip an instance:

    VB Code:
    1. Dim strSrch as String
    2.  
    3. strSrch = strFindBetween("The stove is still on. The house could catch fire!", "The ", "fire", 1)
    4.  
    5. 'This will cause strSrch to equal "house could catch" because it would skip the first "The" instance





    Enjoy, I guess!
    Last edited by BrendanDavis; Oct 27th, 2006 at 01:42 PM.

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