• Log in or sign up for an account here: https://www.sythe.org/login

[Tut]Instr

jdsfighter

Forum Addict
Joined
Jan 21, 2007
Sythe Gold
0
Messages
603
Age
33
Location
USA
[Tut]Instr
Contents
I. Intoduction
II. The Function
.....A. Function Breakdown
III How to Use pt. 1
.....A. Coding
.....B. Breakdown
IV How to Use pt. 2
.....A. Coding
.....B. Breakdown
V. The Mid Statement
.....A. Function Breakdown
.....B. Coding
.....C. Breakdown

Difficulty: Basic - Intermediate

I. Introduction

Have, you ever wanted to parse a web page and get some information from it, but just didn't know how?

To tell you the truth once you learn to do it, it can be one of the simplest things. Although sometimes web pages like to change their content around, and you have to recode, but this will at least teach you what you need to know to parse almost any piece of data. Also it is important to note, that sometimes it is better to use split rather than instr, such an occasion is when the string is like hello|goodbye|Up|Down. Now, do you see the constant pattern of how the | divides the string? That is what split is good for, parsing a string with a constant layout. While Instr on the other hand is useful for pretty much everything else.

II. The Function

A. Function Breakdown
Instr pretty much goes like this
Code:
InStr(Start, String1, String2, [Compare As VbCompareMethod = vbBinaryCompare])

But Visual Basic says
Code:
InStr([Start], [String1], [String2], [Compare As VbCompareMethod = vbBinaryCompare])

But i would just like to say, VB is lieing to you. Start. String1, and String2 are not optional, you will error if you don't put something in there.

Ok now the break down
Start - Used to tell where to start the search.
String1 - The string that you are going the search
String2 - What you are looking for
Compare - How you want to search the string
.....vbBinaryCompare - Contextual, Case Sensitive
.....vbTextCompare - Textual, Not Case Sensitive
.....vbDataBaseCompare - This performs a search based on information in a database

Also it is good to note that instr returns a number not an actual string.

III. How to Use pt.1

A. Coding
I am going to just jump into coding.
Code:
Msgbox InStr(1,"Hello Cruels", "r")

B. Breakdown
Now this above statement is searching the string "Hello Cruels" and I am trying to find "r". Since instr tells me the position of the search it will return 8.

Now I bet you are thinking, oh wow big whoop. I can now successfully see where something is, now how does that help me parse?

That is actually the easy part, but to parse data it takes to points. So say i want to get the letters "uel", then you need to repeat the above process to get "s".


IV. How to Use pt.2

A. Coding
Code:
Dim Point1 as Integer, Point2 as Integer
Point1 = InStr(1,"Hello Cruels", "r") + 1
Point2 = InStr(Point1,"Hello Cruels", "s")

B. Breakdown
Now I know that the above code may look slightly confusing, but it is actually a lot easier than you think.

The first line looks similar to the code I showed you earlier, except for that little "+ 1". That is there so I don't get the position of the "r", also be sure to keep the + # equal to the length of the string you are searching with. So I bet you are wondering why I don't just get the "u". That is because, what if that was a constantly changing string between the "r" and the "s"? It would be pretty much impossible.

On the second line you'll notice that I start searching where the first string leaves off. This is to eliminate the possibility of possibly researching the part that has already been searched.

This code is useful for getting the positions, but now you need something to pull it all together.

V. The Mid Function

A. Function Breakdown
Getting the points is only the first part. Now you need a function to pull it all together. This function is the Mid statement. I am only going to dip in it for a couple moments so you know how to use it, but I will not be covering it in detail.

Mid returns a specified number of characters from a string.

Code:
 Mid(String, Start As Long, [Length])
String - This is the string you wish to parse from
Start - This is where you want to start your search
Length - This is the length of the piece you wish to retrieve

B. Coding
Code:
Dim Point1 as Integer, Point2 as Integer
Point1 = InStr(1,"Hello Cruels", "r") + 1
Point2 = InStr(Point1,"Hello Cruels", "s")
Msgbox Mid("Hello Cruels", Point1, Point2-Point1)

C. Breakdown
Now you already know what the instr statements do, but if you don't know how mid works this is pretty much the breakdown, the first part is for the string you wish to parse from, the second part is where you want to start parsing, the third part is the length of the string you are parsing.

If you don't know anything about math (I sure hope you do), then the reason I am subtracting the second point from the first is to get the length of the string I am trying to get.

FIN


btw this is my first tut, so please be gentle. Also if you find any errors or inconsistencies please don't hesitate to tell me.
 
[Tut]Instr
Good post, though I thought InStr was pretty easy to understand, I suppose it could help people without MSDN. Thanks for sharing your infinite knowledge. :D

Considering this is your first tutorial it is remarkably set out and easy to read, congratulations on a good post. :) I do hope to see more from you in the future.
 
[Tut]Instr
Bravo. Another good explanation on the concept of retrieving a string. Although, for presentation purposes, I would leave out
Well I don't know how to elaborate much more.
 
Runescape Gamblingrunescape gambling
Back
Top