Visual Basic Programming

Page 19

You have really come a long way. Good for you! I hope that you have been having fun. We still have quite a bit left to cover. Now you are going to learn about string manipulation. A string is text, nothing more. Any object with a Text or Caption property uses strings. String manipulation is looking at parts of a string. Visual Basic contains five of these functions, which you learn to use on this page. Some ways of manipulating strings are direct and simple, but other ways are very difficult. I cover four different programs that manipulate strings in this page. First, we begin with a brief introduction.

The most simple function to use is the len (length) function, which you have actually used on two previous pages. This function returns the length of a string, which is the amount of characters and spaces the string contains. For instance:

String Example 1: Good Evening! String Example 2: This is a string.
Length = 13 Length = 17

Now we are going to put these functions to use.

This line says that when the optLen option button is clicked, set the caption of the label lblResult to "Result: " and the Length (Len) of the string lblString.Caption. We could have also stored lblString.Caption in a string variable and place that into the parenthesis. The label displays 41 because there are 41 characters in the string, which includes the text and spaces. You can count them yourself if you would like.

Line 1: Declare the variable strSearch as the string data-type.
Line 2: Set the variable strSearch equal to the text entered by the user in the input box. The input box prompts them to Enter string:, and displays the title Search.
Line 3: Use the InStr function which looks in the string lblString.caption, starting with the first character. It compares the text in the label lblString and the text in the variable strSearch, which is entered into the input box.
Line 4: When the text is the same, display a message box with the message The string was found as the exclamation message type.
Line 5: Otherwise (text was not found), display message in message box The string was not found as the exclamation message type.
Line 6: End the If statement

This line sets the caption of the label lblResult to Result: and the leftmost (Left function) 15 characters in the string lblString.Caption when the user clicks the option button. This amount of characters is from the number in the line.

This line sets the caption of the label lblResult to Result: and the rightmost (Right function) 10 characters in the string lblString.Caption when the user clicks the option button. This amount of characters is from the number in the line.

This line sets the caption of the label lblResult to Result:, then the thirteenth character from the left, and it gets six characters from that point. 13 is the start and 6 is the length, or the amount of characters to get from character 13.

Test the application if you would like. Now we will do something a little more useful. We are going to re-created the common Windows application Notepad. This will be almost the same as Notepad, with a few minor differences, such as no Undo option in the menu. We may cover topics that relate to the Undo function later in this tutorial.

Proceed to Page 20 to begin the text editor.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30