String data is a cornerstone of almost any program. Names , addresses, almost any piece of information that isn't purely numeric is represented as a string. If you recall from our discussion of data types in Chapter 4, "Data Types and Numeric Variables ," string data is simply one or more characters treated as a single data item. As we'll see in this chapter, however, using the String data type is a bit different from the numeric data types inherent in Visual Basic .NET.
The String data type is implemented in Visual Basic .NET as a string class rather than as an inherent structure like the numeric data types. Because string variables are class objects, you can work with string data on two different levels. First, you can manipulate the string data using functions that are built into Visual Basic .NET (we'll study these functions first in this chapter). Second, because strings are implemented as class objects in the .NET framework, you can also use the methods that are inherent in the string class to manipulate the data.
Text Boxes and Strings
You've seen in earlier programs that you can type characters into a text box as the program runs. The sequence of characters you type into the text box is treated as a string. Consider the following program lines:
Dim MyString as String MyString = txtString1.Text
The first statement creates a String variable named MyString and finds a place for it in memory. The next program statement takes the characters you typed into the txtString1 text box and assigns them as a String into MyString . In this example, the Text property of the txtString1 text box holds the string data that you typed in from the keyboard as the program executed. Because the Text property of the txtString1 text box can change according to whatever the user wants to type into the text box, the Text property behaves like a String variable.
Now consider a slight modification to these two program lines:
Dim MyString as String MyString = "Lunch time"
In this situation, the content of MyString is assigned a fixed sequence of characters. Unless something later in the program modifies MyString , its contents always remain "Lunch time" throughout the life of the program. Anytime you see a sequence of characters surrounded by double quotation marks as shown in the preceding code, the double-quoted sequence of characters is referred to as a string literal. In this case, we're assigning a string literal into a string variable named MyString .
Manipulating Visual Basic .NET String Data
Perhaps the best way for you to learn how to work with strings is to write a simple program in Visual Basic .NET that manipulates a string for you. To write the program, start a new project and call it MyStringProgram. Add four label controls, six text box controls, and two button controls as shown in Figure 6.1. The names for the various text boxes are as shown in the figure. The Calculate button is called btnCalc and the Exit button is named btnExit . The names you give to the label controls aren't important because they aren't actually referenced by any expressions in the program.
Figure 6.1. Control layout for MyStringProgram.
Add the following line to the btnExit Click event:
You'll recall from previous programs that this line releases all the resources used in the program when we're finished with them and terminates the program. The keyword Me is simply a shorthand notation for the currently active form. Because our program has only one form, releasing the form ends the program in a graceful manner.
After you've added the necessary controls and code to the form, save the project. We'll make minor changes to the program throughout this chapter to show you how you can manipulate string data in Visual Basic .NET.
Perhaps one of the simplest operations on string data is to add two strings together to form a new (longer) string. The process of adding two strings is called string concatenation. You can concatenate strings by adding one line of code to the btnCalc Click event:
txtResult.Text = txtString1.Text + txtString2.Text
This statement simply takes the string held in the txtString1 text box and adds it to the string held in the txtString2 text box. The statement then assigns the new string into the txtResult text box.
Now compile and run the program. A sample run is shown in Figure 6.2.
Figure 6.2. Sample run of MyStringProgram using string concatenation.
What isn't obvious from the sample run is that there is a blank space in the txtString1 text box after the word Joyce . If we had not added this blank space, the result would look like a single word JoyceScarfo rather than Joyce Scarfo .
Because string concatenation is a fairly simple string process, we didn't need to use the other text boxes on the form. We'll make use of them in later sections.
Shorthand Operator for String Concatenation
String concatenation is commonly used to build a single longer string from a series of smaller strings. In fact, string building is such a common thing, Visual Basic .NET provides a shorthand operator for it. You could build the string with the statement:
txtResult.Text = txtResult.Text & txtString2.Text
But that's sort of like sliding down a staircase on the stairs. Verbally, the statement says, "Take the string in txtResult and add to that string a second string named txtString2 and take the result of the string concatenation of the two strings and reassign it into txtResult ." Makes me tired just saying it.
Instead, you can simply use
txtResult.Text &= txtString2.Text
Notice the &= operator in the middle of the statement. If we verbalize this statement, it says, "Take the string in txtResult and add txtString2 to it." This shorthand operator for string concatenation saves you from typing the txtResult.Text operand in the expression a second time. This shorthand operator simplifies the statement and it uses the preferred string concatenation operator ( & ).
In some programs, you'll need to know how many characters the user typed into a text box or a string. The number of characters held in a string variable is known as the length of the string. To show you how to calculate the length of a string, remove the old program statement in the btnCalc Click event and replace it with the following new statement:
txtResult.Text = CStr(Len(txtString1.Text))
This program statement says to use the Len() function that's built into Visual Basic .NET to calculate the length of the string held in the txtString1 text box. The Len() function returns the number of characters in the text box.
However, there is a problem. Because the data type returned from Len() is a numeric data type, we must use the CStr() function to convert the numeric value returned by Len() to a String data type before assigning the result into txtResult.Text . Doing so forces the data type on the right side of the assignment operator (that is, the equal sign) to match the data type on the left side of the assignment operator (or string-into-string assignment, in this expression).
Although the data type conversion using the CStr() function isn't required in this example, it's always a good idea to have the data types match when using the assignment operator. There are instances in which Visual Basic .NET does not automatically convert from one data type to another. In those cases where the types do not match, Visual Basic .NET might get a little cranky and tell you there is a type mismatch in the program expression. Whenever you see such an error message, you'll probably need to use one of the conversion functions to make the data types used in the expression match. (You can find a list of these conversion functions in Table 4.3 in Chapter 4. The conversion functions start with the letter C followed by the type they convert to, such as CBool , CByte , CChar , and so on. You can also use the generic CType function, too.) We'll use many of these conversion functions in later chapters.
Figure 6.3 shows a sample run using the Len() function. In this sample run, the txtResult text box shows the number of characters present in the txtString1 text box. Because Len() can return the length of only one string at a time, the other text boxes in the program aren't used.
Figure 6.3. Sample run using the Len() function.
One more thing about the Len() function. It does not return the number of bytes of storage used for the string. Remember that strings are stored using Unicode, which is a double-byte character set. Also, some overhead bytes are associated with strings that exist independently of the string content. Therefore, you should not equate string length with string storage requirements. They are not the same.
There are times when you'll want to extract part of one string and make it a new string. For example, suppose that the user types in Mr. Jim Hallet , but you don't want the Mr . part of the string. Instead, you want just Jim Hallet . Stated another way, you want a substring of Mr. Jim Hallet that is simply Jim Hallet . Visual Basic .NET provides you with several built-in functions that can be used to extract a substring from a string.
The Left() Function
The Left() function has the following syntax requirements:
Substring = Microsoft.VisualBasic.Left( Str, LenOfSubstr )
First of all, what's this Microsoft.VisualBasic . stuff that comes before the Left() function all about? Well, it's there to avoid confusing Visual Basic .NET. The problem is that Left() isn't only a string manipulation function; it's also the name of a property in a Windows form. For example, if you want to place a form named frmMyForm 200 pixels from the left edge of the screen, you could use the program statement:
frmMyForm.Left = 200
In this statement, Left is a property of frmMyForm . By fully qualifying Left with Microsoft.VisualBasic.Left , Visual Basic .NET knows that you want to use the built-in Left() string function, not a Visual Basic .NET property named Left .
Note that there are two arguments to the Left() function. The first is the string we want to use. The second argument is an integer value that tells Visual Basic .NET how many characters from the start of the string to use when forming the new substring. For example, if the expression is
SubStr = Microsoft.VisualBasic.Left("Aunt Nancy", 4)
the variable SubStr would be assigned the string value Aunt .
With that information in mind, remove the old program statement from the btnCalc Click event and change it to
txtResult.Text = Microsoft.VisualBasic.Left(txtString1.Text, _ CInt(txtArg1.Text))
Note that we use the CInt() conversion function to convert the text in the txtArg1 text box into an integer variable, as required for the second argument used in the Left() function.
Figure 6.4 shows a sample run of the program.
Figure 6.4. Sample run of Left() function.
The sample run shows what happens when we extract the leftmost six characters from Brighteyes and treat the result as a substring, as shown in the txtResult text box.
The Mid() Function
The Mid() function is used when you want to extract a substring from a position other than at the beginning of the string. The syntax for the Mid() function is
Mid( String, Start, [ length ])
Notice that we've placed the third argument (that is, length ) in square brackets. Square brackets around an argument mean that it's an optional argument to the function. The first argument (that is, String ) is the string from which we want to extract the substring. The second argument ( Start ) is the starting position for the substring. If you supply the optional length argument, that many characters are returned in the substring. If you do not supply a length argument, everything to the right of Start is returned as the substring.
SubStr = Mid("James Earl Jones", 7, 4)
would assign Earl into SubStr because we want to start our substring with the seventh character (that is, the E ), but only return four characters. On the other hand,
SubStr = Mid("James Earl Jones", 7)
would assign Earl Jones in SubStr because no length argument is given. In this example, everything to the right of character position seven is returned as the substring. Note that we do not need to supply the Microsoft.VisualBasic . in front of the Mid() function because there is no form property named Mid .
To test the Mid() function, remove the current line from the btnCalc Click event and replace it with the following lines:
If txtArg2.text = "" Then txtResult.Text = Mid(txtString1.Text, CInt(txtArg1.Text)) Else txtResult.Text = Mid(txtString1.Text, CInt(txtArg1.Text), _ CInt(txtArg2.Text) ) End If
Unfortunately, we're forced to use a Visual Basic .NET language construct that we haven't studied yet: the If - Then - Else statement. Simply stated, the If statement tests to see whether the user typed in something into the txtArg2 text box. If the txtArg2 text box does not contain any characters (that is, an empty string or "" ), the user wants to return everything from the starting character to the end of the string. If the user does type something into the txtArg2 text box, we assume that he is supplying a length argument to the Mid() function.
Figure 6.5 shows a sample run in which the user has supplied a length argument; Figure 6.6 shows the same run, but without a length argument. Notice how the substring is affected.
Figure 6.5. Mid() function with a length argument.
Figure 6.6. Mid() function without a length argument.
Figure 6.6 shows that the remainder of the string is returned when the optional third argument is not supplied.
The Right() Function
It should come as no surprise that if there is a Left() function, there's probably a Right() function, too. The syntax for the Right() function is
Right( String, Length )
The Right() function does not need a starting position because the starting position is calculated from the Length argument. For example, suppose that the Length argument equals 5 . What the Right() function really says is, "Give me the rightmost five characters of the string." In other words, the starting position is counted backward from the end of the string for Length characters. Therefore, the program statement
SubStr = Right ("James Earl Jones", 5)
would find SubStr equal to Jones because that is the last five characters in the string.
Remove all the current program statements from the btnCalc Click event and replace them with
txtResult.Text = Microsoft.VisualBasic.Right(txtString1.Text, _ CInt(txtArg1.Text))
Again, because Windows forms have a Right property, we need to fully qualify the Right() function so Visual Basic .NET doesn't get confused . As usual, we need to use the CInt() conversion function to convert the length argument held in txtArg1.Text as a string into an Integer data type. Figure 6.7 shows a sample run for the Right() function.
Figure 6.7. Sample run of the Right() function.
Notice that the rightmost four characters from the string are returned as a substring.
The InStr() Function
There will be times when you need to determine the starting position of a substring within a larger string. For example, you might want to extract the last name from a string that holds the first name followed by the last name. In this case, the string might be James Bond and you want to create a substring using only the last name, Bond . That's exactly what the InStr() function is designed to do.
The syntax for the InStr() function is
StartPosition = InStr( StringToUse, SubstringToFind )
The first argument to the InStr() function is the string we want to search, and the second argument is the substring we want to find. Note that the substring we want to find can hold one or more characters. The value returned by InStr() is the starting position of the first instance of the substring being searched. In our example, we would use
StartPosition = InStr("James Bond", " ")
which says we want to locate the blank space substring between the first and last names.
If InStr() can find a match on the search substring, it returns the starting position for the substring. In the preceding example, InStr() would return 6 . If InStr() cannot find a match on the substring, it returns to indicate that no match was found in the string. (As a test of your understanding, how would you use the value in StartPosition to return the last name in the string? Hint: Think about the Mid() function and keep in mind that most last names don't start with a blank space.)
To test the InStr() function, remove all the current program statements from the btnCalc Click event and replace them with
txtResult.Text = InStr(txtString1.Text, txtArg1.Text)
and compile and run the program. Figure 6.8 shows the results of a sample run. Unfortunately, you can't see that I typed in a blank space for txtArg1 , but that's the substring we're searching for.
Figure 6.8. Sample run using the InStr() function.
You might also rerun this program, but change the search string from a blank space to "on" and study the results. Also, try entering a substring that does not exist in the string and observing the results.
Working Directly on the String Data Type
All the string functions that we've discussed thus far work directly on the String data using functions that are built into Visual Basic .NET. The string manipulations we've examined all require that you specify the string as an argument to the function. Stated in terms of what we discussed in Chapter 2, each of these string manipulation functions needs the lvalue of the string passed to the function as a function argument. In other words, these string functions are sitting out there in memory somewhere waiting to help you. All you have to do is pass them some information about the string and what you want to do with it.
In the next section of this chapter, we discuss string operations as they exist within the String class. The string operations performed on the String class work a little differently, even though they can provide the same functionality as the String functions discussed earlier in this chapter. The String class methods provide more ways to manipulate String data and are often easier to use.