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.
Functions
Len --> Returns a number which is the amount of spaces and characters in a string
InStr --> Looks to see what is in the string
Left --> Looks at the left-most characters in a string; the programmer specifies how many
Right --> Looks at the right-most characters in a string; the programmer specifies how many
Mid --> Uses a starting character position, then looks at a certain amount to the right from the starting point; the programmer specifies how many
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.
Set up the form above.
Name the top label lblString.
Name the option buttons as follows from left to right:
optLength
optInstr
optLeft
optRight
optMid
Name the bottom label lblResult.
Lock the controls on the form.
That was easy... now double-click the optLen option button.
Press tab, then type the following lines:
lblResult.Caption = "Result: " & Len(lblString.Caption)
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.
Click on the object box in the code window and select optInstr.
Press tab, then type the following lines:
Dim strSearch As String
strSearch = InputBox("Enter string:", "Search")
If InStr(1, lblString.Caption, strSearch) Then
MsgBox "The string was found.", vbExclamation, ""
Else: MsgBox "The string was not found.", vbExclamation, ""
End If
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
Click on the object box in the code window and select optLeft
Press tab, then type the following line:
lblResult.Caption = "Result: " & Left(lblString.Caption, 15)
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.
Click on the object box in the code window and select optRight.
Press tab, then type the following line:
lblResult.Caption = "Result: " & Right(lblString.Caption, 10)
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.
Click on the object box in the code window and select optMid.
Press tab, then type the following line:
lblResult.Caption = "Result: " & Mid(lblString.Caption, 13, 6)
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.