Results 1 to 4 of 4

Thread: [VB6] Writing and Understanding the GetBetween

  1. #1
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m

    [VB6] Writing and Understanding the GetBetween

    Introduction:

    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:

    Code:
    Public Function GetBetween(Main As String, Start As String, Finish As String, Optional Index As Long = 1) As String
    
    'Place GetBetween code here...
    
    End Function
    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 (you need an account to see links).

    Code:
    Index = InStr(Index, Main, Start)
    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".
    Code:
    InStr(1, "Zach Zach", "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).
    Code:
    InStr(3, "Zach Zach", "a")
    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")
    Code:
    InStr(1, "abcdef", "cde")
    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,
    Code:
    InStr(1, "abcdef", "cde")
    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:
    Code:
    InStr(1, "abcdef", "cde") + Len("cde")
    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 (you need an account to see links), 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.

    Code:
    If Index = 0 Then
    GetBetween = vbNullString
    Else
    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 (you need an account to see links).
    This code returns 5:
    Code:
    Len("12345")
    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()!

    Code:
    Index = Index + Len(Start)
    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!

    Code:
    Dim lngStop As Long 
    lngStop = InStr(Index, Main, Finish)
    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 (you need an account to see links). 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:
    Code:
    If lngStop = 0 Then
    GetBetween = vbNullString
    Else
    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...

    Code:
    GetBetween("abc123def", "abc", "def")
    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 (you need an account to see links) 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: ((you need an account to see links))

    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.

    Code:
    Mid$(String to Grab From, Start Grabbing Index, Length to Grab)
    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.

    Code:
    GetBetween = Mid$(Main, Index, lngStop - Index)
    Final Code:

    Code:
    Public Function GetBetween(Main As String, Start As String, Finish As String, Optional Index As Long = 1) As String
    Index = InStr(Index, Main, Start)
    If Index = 0 Then
        GetBetween = vbNullString
    Else
        Index = Index + Len(Start)
        Dim lngStop As Long
        lngStop = InStr(Index, Main, Finish)
        If lngStop = 0 Then
            GetBetween = vbNullString
        Else
            GetBetween = Mid$(Main, Index, lngStop - Index)
        End If
    End If
    End Function
    Conclusion:

    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.

    -Zach

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

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

  3. #2
    Saiyan Race
    j03's Avatar
    Joined
    Dec 2011
    Posts
    13,720
    Userbars
    166
    Thanks
    5,906
    Thanked
    33,076/6,608
    DL/UL
    23/36
    Mentioned
    3,867 times
    Time Online
    563d 4h 55m
    Avg. Time Online
    3h 14m
    This is the ultimate string handling test. If you can write your own GetBetween/GetStringBetween and understand it, you're set.
    (you need an account to see links)
    (you need an account to see links)(you need an account to see links)

    ------------------------
    [02/24/2013] Stealth CORE is made into the first standalone Neopets auto-player.
    ------------------------


  4. #3

    Joined
    Dec 2011
    Posts
    488
    Thanks
    303
    Thanked
    559/263
    DL/UL
    13/4
    Mentioned
    34 times
    Time Online
    1h 58m
    Avg. Time Online
    N/A
    Definitely will help newer programmers.

  5. #4

    Joined
    Jan 2014
    Posts
    54
    Userbars
    1
    Thanks
    6
    Thanked
    8/8
    DL/UL
    24/0
    Mentioned
    10 times
    Time Online
    1d 23h 5m
    Avg. Time Online
    N/A
    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
  •