[VB6] GetStringBetween Function

Discussion in 'Code Snippets and Tutorials' started by Smelly, Aug 18, 2007.

  1. Smelly

    Smelly Level IV

    Joined:
    Dec 1, 2006
    Messages:
    2,197
    Likes Received:
    8
    Location:
    England
    Quite a few neopets programs require a GetStringBetween function when you're making them. If you don't know it already here it is, to save you scouring the net to try and find it. Just paste it into the main form code.

    Code (Visual Basic):
    1. <div class="vb" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #b1b100;">Public</span> <span style="color: #b1b100;">Function</span> GetStringBetween<span style="color: #66cc66;">(</span>ByVal InputText <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span>, _</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; ByVal StartText <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span>, _</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; ByVal EndText <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span>, _</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; Optional ByVal StartPosition = <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">)</span> <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">String</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #b1b100;">Dim</span> lnTextStart <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Long</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #b1b100;">Dim</span> lnTextEnd <span style="color: #b1b100;">As</span> <span style="color: #b1b100;">Long</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; lnTextStart = <span style="color: #b1b100;">InStr</span><span style="color: #66cc66;">(</span>StartPosition, InputText, StartText, <span style="color: #b1b100;">vbTextCompare</span><span style="color: #66cc66;">)</span> + <span style="color: #b1b100;">Len</span><span style="color: #66cc66;">(</span>StartText<span style="color: #66cc66;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; lnTextEnd = <span style="color: #b1b100;">InStr</span><span style="color: #66cc66;">(</span>lnTextStart, InputText, EndText, <span style="color: #b1b100;">vbTextCompare</span><span style="color: #66cc66;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #b1b100;">If</span> lnTextStart >= <span style="color: #66cc66;">(</span>StartPosition + <span style="color: #b1b100;">Len</span><span style="color: #66cc66;">(</span>StartText<span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #b1b100;">And</span> lnTextEnd > lnTextStart <span style="color: #b1b100;">Then</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; GetStringBetween = <span style="color: #b1b100;">Mid</span>$<span style="color: #66cc66;">(</span>InputText, lnTextStart, lnTextEnd - lnTextStart<span style="color: #66cc66;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #b1b100;">Else</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; GetStringBetween = <span style="color: #ff0000;">""</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; </li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li></ol></div>
     
  2. Skyle

    Skyle Level II

    Joined:
    Aug 13, 2007
    Messages:
    212
    Likes Received:
    7
    Example of how to use it:

    Code (Text):
    1. <div class="text" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">Call GetStringBetween(PageSource, "rightbeforethethingyouwant", "rightafterthethingyouwant")</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li></ol></div>
    Page source would be where the HTML is stored, and just find the text before the text you want to parse and the text right after.

    It will be a good function for all newbies.