Results 1 to 4 of 4

Thread: [VB6] Writing and Understanding the GetBetween

  1. #1
    Little Brother

    Zachafer's Avatar
    Join Date
    Dec 2011
    Thanked 1,326 Times in 614 Posts

    479 Post(s)
    Time Online
    20 d 12 h 43 m
    Avg. Time Online
    20 m
    Rep Power

    [VB6] Writing and Understanding the GetBetween


    Hello everyone! In this tutorial, I will explain to you how a GetBetween function works and how you can write your very own GetBetween.

    What is a GetBetween:

    First of all, a GetBetween is a function that is commonly used in Neopets programming that allows the programmer to grab a string (usually in a HTML chunk) that is located in between two other strings. Here is an example:

    GetBetween("a123b", "a", "b") gets the text that is between the string "a" and the string "b". Thus returning "123".

    How GetBetween Works:

    GetBetween's work in a very simple matter. Here is the order in which they work:
    1. Find the position of the starting word (in the example I used "a").
    2. Find the position of the ending word (in the example I used "b").
    3. Use Mid$() to get the text in the middle of the two string's positions.
    Most GetBetween's check if the starting word and the ending word actually exist in the main string before trying to get the middle text using Mid$().

    Creating our own GetBetween:

    Most people make their own GetBetween's, and there is no reason for you not to be able to do the same. It is considered an "intermediate task" amongst some programmers, but once you understand how they work, it really isn't that hard.

    As you may or may not know, all functions must be declared differently then subs. Here is the GetBetween function structure we will be using:

    Step 1: Finding the Starting Word's Position

    The first step in making our own GetBetween is to find the position of the starting word. We are going to be using InStr().

    This code right here searches the Main string for the Start string. You may notice that Index is the first parameter. The first parameter (optional) of InStr() is the starting index to search at. Let me give you an example:
    This returns 2, because we are starting our search at the first position and looking for an "a".
    This returns 7 because the second "a" is in the 7th position and we start our search at the 3rd position (the first "a" is in the second position).
    InStr() returns the position of the start of the word and not the end of the word.
    This code would return 3 and not 6 because it returns the starting position of the word.
    (3 is the index of "c" and 6 is the index of "e")
    The way we fix this is that we add the length of the starting word to the position of the start of the word. Let's see an example:
    As you know,
    would return 3 (the index of "c") and not 6 (the index of "e").
    But the length of "cde" is 3, so if we do:
    then we will have 6. I will continue talking about this in the next part...
    Step 2: Checking that the Starting Word Exists

    According to MSDN's InStr() article, when a needle is not found in the haystack (when the starting word isn't found in the Main string), then InStr() returns 0. So we are going to add a check that sees if InStr() returned 0.

    So, no we are checking to see if InStr() returned 0, and if it did, then we will return an empty string, but if it didn't we will continue on with our code.

    Step 3: Adding the Length of the Starting Word to the Starting Word's Position

    To find the length of a string, we use a built-in function called Len().
    This code returns 5:
    because the length of "12345" is 5.
    So right now, Index is the position of the first letter in the starting string, right? But we need to find the position of the last letter in the starting string! This is made possible with Len()!

    We are basically adding the length of the starting string to Index and then storing that data back into the Index (long) variable.

    Step 4: Finding the Finishing Word's Position

    Now we must find where the stopping word is located in the main string. This is important because if we can find the string that is in between the Starting and the Finishing word, then our GetBetween function is a success!

    So let's take a small break here. Index originally starts out at 1 (assuming we use the default value for Index). Then, Index becomes the position of the starting word. And then, we added the length of the starting word to Index, so now Index is the position of the last letter in the starting string. So when we put Index into the first parameter of InStr(), it starts searching at the starting word. Another way to say it is that it is only looking past the starting word.

    Step 5: Checking the Finishing Word Exists

    Now that we have found the position of the finishing word, we should make it sure that it actually is existent in the Main string to avoid any errors. We can do this really easily by reading MSDN's InStr() article. This article tells us that InStr() returns 0 when the 'needle' is not found in the 'haystack' (Finish = needle, Main = haystack).

    So a simple if statement will work fine:Step 6: Explanation of Mid$()

    Now on to the last step! You've made it this far! (Congrats )

    So now that we know:
    • The position of the last letter of the starting word in the main string (Index)
    • The position of the first letter of the finishing word in the main string (lngStop)
    Now when we think about this logically, we have the last letter and the first letter of two important strings...

    So if we know that 3 is the Index of the last letter in the starting word ("c") and that 7 is the Index of the first letter in the finishing word ("d"), then if we can find the text that is between 7 and 3 (or 3 and 7) then we have the final code for GetBetween! :phone:

    So, we use the Mid$() function. (Mid() and Mid$() are the same thing, except for the fact that Mid() returns variants (slowest variable type in BASIC) and that Mid$() returns a string. More information here: (

    Now, let me try to explain to you how Mid$() works.

    Mid$() has a pretty simple syntax, it is fairly easy to understand. We are going to be using 3 parameters.

    String to Grab From:

    The string to grab from is the string that we called Main in our GetBetween function structure. It is the Main string where both the Start and Finish strings are supposed to be located.

    Start Grabbing Index:

    The index to start grabbing from is the second parameter in the Mid$() function. We use it to tell our BASIC compiler where we want to start grabbing the text.

    Length to Grab:

    This tells our BASIC compiler how many characters we want to grab. After reading all of this, you should probably get an idea of how we are going to be using this Mid$() function along with our previously attained data.

    Step 7: Getting the Final String

    Now that we have all the materials needed to get the middle string, we use Mid$() to extract it from the Main String.

    Final Code:


    I hope this guide as helped you in many ways. You learned a lot about string manipulation.

    This may not be the fastest GetBetween, but it makes the most sense to people and is easy to understand.


  2. BuyNeopointsCheap

  3. The Following 8 Users Say Thank You to Zachafer For This Useful Post:

    DarkAngel (06-29-2012),hectorvazc (11-24-2012),Infamous Joe (03-11-2012),jojo (03-18-2012),Miguel (03-11-2012),prison break (03-27-2012),Reemer (03-11-2012),xRomanChan (04-11-2012)

  4. #2
    Saiyan Race

    Infamous Joe's Avatar
    Join Date
    Dec 2011
    Hyperbolic Time Chamber
    Thanked 13,393 Times in 3,573 Posts

    2756 Post(s)
    Time Online
    241 d 11 h 48 m
    Avg. Time Online
    3 h 56 m
    Rep Power
    Gamer IDs

    Gamertag: Infam0us Joe PSN ID: makavelien Steam ID: infamousjoe
    This is the ultimate string handling test. If you can write your own GetBetween/GetStringBetween and understand it, you're set.

  5. #3
    Join Date
    Dec 2011
    Thanked 549 Times in 260 Posts

    34 Post(s)
    Time Online
    1 h 58 m
    Avg. Time Online
    Rep Power
    Definitely will help newer programmers.

  6. #4

    Join Date
    Jan 2014
    Thanked 8 Times in 8 Posts

    10 Post(s)
    Time Online
    1 d 23 h 5 m
    Avg. Time Online
    2 m
    Rep Power
    this guide helps alot! thanks

Posting Permissions

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