There are five new string functions built into the Visual Basic language:
Figure 3-6 shows a sample application, StringFun.VBP, which demonstrates each of the new functions. Each of the tasks performed by StringFun.VBP is described in the sections that follow.
Figure 3-6. Click on each of the command buttons in
StringFun.VBP to see a demonstration of each of the new string functions.
Replacing Characters in a String
One of the most common programming tasks is converting text from one format to another. Most conversions require replacing one set of delimiting characterssuch as tabs, spaces, or bracketswith another. The Replace function handles those types of tasks in a single step. The following procedure shows how to use Replace to perform a very simple conversion by replacing spaces with carriage returns and vice versa.
`cmdReplace_Click uses Replace to
`do a global search and replace
Private Sub cmdReplace_Click()
Static blnReplaced As Boolean
`Replace spaces with carriage returns
If Not blnReplaced Then
txtString = Replace(txtString, " ", vbCrLf)
`Replace carriage returns with spaces
Else
txtString = Replace(txtString, vbCrLf, " ")
End If
blnReplaced = Not blnReplaced
End Sub
Breaking and rejoining strings is commonly used when gathering and reordering information in a text box. The Split and Join functions can be used together to break text into words, reverse their order, and display them, as shown by the procedure below.
`cmdReverse uses Split and Join to
`reverse the order of words in a text box
Private Sub cmdReverse_Click()
Dim strForward() As String
Dim strReverse() As String
Dim intCount As Integer
Dim intUpper As Integer
`Tokenize the text
strForward = Split(txtString, " ")
`Initialize the other array
intUpper = UBound(strForward)
ReDim strReverse(intUpper)
`Reverse the order of words in strReverse
For intCount = 0 To intUpper
strReverse(intUpper - intCount) = strForward(intCount)
Next
`Detokenize the text
txtString = Join(strReverse, " ")
End Sub
The key to understanding Split and Join is to think of them as conversion functions. Split converts a string into an array of strings and Join converts an array of strings into a single, continuous string. Once you've converted a
string into an array of items, you can sort the list using standard array sorting procedures, or you can select items from the list by applying a filter, as described in the next section.
Applying Filters
The Filter function takes an array of strings and returns an array of elements that contain a specific substring. The Filter function is commonly used with the Split and Join functions to process lists of items in a text box or other text source. The procedure below shows how to apply a filter to words in a text box.
`cmdFilter_Click uses Filter to display only
`the words containing a specific set of characters
`This is useful when narrowing a list of items based
`on what a user types
Private Sub cmdFilterClick()
Dim strFilter As String
Dim strWords() As String
`Get a substring to check for
strFilter = InputBox("Show only words containing:")
`If cancelled, then exit
If strFilter = "" Then Exit Sub
`Get rid of carriage returns
txtString = Replace(txtString, vbCrLf, " ")
`Split string into an array of single words
strWords = Split(txtString, " ")
`Get a list of the words containing strFilter
strWords = Filter(strWords, strFilter)
`Display the list in the text box
txtString = Join(strWords)
End Sub
Finding a word or phrase in a block of text is a common text-editing task that most users expect from word processors or when presented with a large amount of text online. The InStr function provides the ability to search forward within a block of text and now the InStrRev function lets you reverse that search direction. The two procedures below demonstrate these types of searching within a text box.
`cmdBack_Click and cmdForward_Click
`use InStr and InStrRev to perform forward and
`backward searches through a text box.
`These techniques can be used to selectively
`replace text
Private Sub cmdBack_Click()
Dim strFind As String
On Error GoTo errNotFoundRev
`Get a string to find
strFind = InputBox("String to find:", "Search Backward")
txtString.SelStart = _
InStrRev(txtString, strFind, txtString.SelStart) - 1
txtString.SelLength = Len(strFind)
Exit Sub
errNotFoundRev:
MsgBox strFind & " not found.", vbInformation
End Sub
Private Sub cmdForward_Click()
Dim strFind As String
On Error GoTo errNotFoundFor
`Get a string to find
strFind = InputBox("String to find:", "Search Forward")
txtString.SelStart = _
InStr(txtString.SelStart + 1, txtString, strFind) 1
txtString.SelLength = Len(strFind)
Exit Sub
errNotFoundFor:
MsgBox strFind & " not found.", vbInformation
End Sub
Strangely, the InStr and InStrRev functions order their arguments differently. In InStr, the start position of the search is the first argument and in InStrRev the start position is the third argument. Because of this inconsistency, it is easy to confuse the arguments when using these functions together.