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:
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$().
- Find the position of the starting word (in the example I used "a").
- Find the position of the ending word (in the example I used "b").
- Use Mid$() to get the text in the middle of the two string's positions.
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:
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 returns 2, because we are starting our search at the first position and looking for an "a".
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")
As you know,
But the length of "cde" is 3, so if we do:
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.
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:
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()!
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!
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:
Now on to the last step! You've made it this far! (Congrats )
So now that we know:
Now when we think about this logically, we have the last letter and the first letter of two important strings...
- 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)
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: (http://www.aivosto.com/vbtips/stringopt.html#variant)
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.
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.
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.
This is the ultimate string handling test. If you can write your own GetBetween/GetStringBetween and understand it, you're set.
Definitely will help newer programmers.
this guide helps alot! thanks