Previous Page
Next Page

Using Basic InStr Step-by-Step Exercises

In this section, you play with the InStr function to become familiar with the basic features of its implementation. Because this is a short script, you don't need to implement a full Header information section.

1.
Open the \My Documents\Microsoft Press\VBScriptSBS\Templates\blankTemplate.vbs template in Notepad or your favorite script editor and save the file as YourNameInstr1.vbs.

2.
Create a variable called searchString and set it equal to 5. Your line will look like the following:

searchString = "5"

3.
Create another variable called textSearched and set it equal to 123456789. Your second line will look like this:

textSearched = "123456789"

4.
Create a third variable called InStrReturn and set it equal to the following InStr command: InStr (textSearched, searchString). This line will look like the following:

InStrReturn = InStr (textSearched, searchString)

5.
Use the WScript.Echo command to print out the results of the InStr command. This line will look like the following:

WScript.Echo (InStrReturn)

6.
Save the file.

7.
Run the YourNameInstr1.vbs file by double-clicking it. You should see a dialog box with the number 5 printed in it. This indicates that search string 5 was found in the fifth position of the script.

8.
Open the \My Documents\Microsoft Press\VBScriptSBS\Templates\BlankTemplate.vbs template in a script editor save it as YourNameInstr2.vbs.

9.
Create a variable called searchString and set it equal to 5. Your line will look like the following:

searchString = "5"

10.
Create another variable called textSearched and set it equal to 123456789. Your second line will look like this:

textSearched = "123456789"

11.
Create a third variable called InStrReturn and set it equal to the following InStr command: InStr (1, textSearched, searchString, 0). This line will look like the following:

InStrReturn = InStr (1, textSearched, searchString, 0)

12.
Use the WScript.Echo command to print out the results of the InStr command. This line will look like the following:

WScript.Echo InStrReturn

13.
Run YourNameInstr2.vbs by double-clicking it. You should see a dialog box with the number 5 printed in it. This indicates that the search string 5 was found in the fifth position of the script when you started looking from the first position of the search string.

14.
Change the 1 to a 5 in your InStrReturn line. It will look like the following:

InStrReturn = InStr(5, textSearched, searchString, 0)

15.
Save your work.

16.
Run YourNameInstr2.vbs by double-clicking it. You should see a dialog box with the number 5 printed in it. This indicates that the search string 5 was found in the fifth position of the script when you started looking from the fifth position of the search string.

17.
Change the 5 to a 6 in your InStrReturn line. It will look like the following:

InStrReturn = InStr(6, textSearched, searchString, 0)

18.
Save your work.

19.
Run YourNameInstr2.vbs by double-clicking it. You should see a dialog box with the number 0 printed in it. This indicates that the search string 5 was not found in the search string when you started looking from the sixth position of the search string.


Previous Page
Next Page