String Array Functions
(Split, Join, and Filter)
VB 6 introduced a trio of powerful functions that operate on string arrays. These functions save the coding effort of having to set up loops and using combinations of other basic string functions to perform the equivalent tasks.
The string array functions are:
Function |
Description |
Split |
Splits a string into separate elements based on a delimiter (such as a comma or space) and stores the resulting elements in a zero-based array |
Join |
Joins (concatenates) elements of an array into an output string |
Filter |
Returns a zero-based array containing subset of a string array based on a specified filter criteria. |
The MSDN definitions for these functions follow:
Function: |
Split
|
||||||||||
Description: |
Returns a zero-based, one-dimensional array containing a specified number of substrings.
|
||||||||||
Syntax: |
Split(expression[, delimiter[, count[, compare]]]) The Split function syntax has these parts:
|
Function: |
Join
|
||||||
Description: |
Returns a string created by joining a number of substrings contained in an array.
|
||||||
Syntax: |
Join(list[, delimiter]) The Join function syntax has these parts:
|
Function: |
Filter
|
||||||||||
Description: |
Returns a zero-based array containing subset of a string array based on a specified filter criteria.
|
||||||||||
Syntax: |
Filter(InputStrings, Value[, Include[, Compare]]) The Filter function syntax has these parts:
|
||||||||||
Remarks: |
If no matches of Value are found within InputStrings, Filter returns an empty array. An error occurs if InputStrings is Null or is not a one-dimensional array. The array returned by the Filter function contains only enough elements to contain the number of matched items. |
A "Try It" example has been set up to demonstrate how these three functions might be used.
Suppose you were given an input string of comma-delimited names, such as:
Abby,Bubba,Charlie,Debbie,Edgar
and you wanted to "weed out" only the names that contained a double "b" ("bb") and output the results as a similar comma-delimited string:
Abby,Bubba,Debbie
The "Try It" code to accomplish this is shown below:
Private Sub cmdTryIt_Click()
Dim strInputString As String
Dim strFilterText As String
Dim astrSplitItems() As String
Dim astrFilteredItems() As String
Dim strFilteredString As String
Dim intX As Integer
strInputString = InputBox("Enter a comma-delimited string of items:", _
"String Array Functions")
strFilterText = InputBox("Enter Filter:", "String Array Functions")
Print "Original Input String: "; strInputString
Print "Split Items:"
astrSplitItems = Split(strInputString, ",")
For intX = 0 To UBound(astrSplitItems)
Print "Item("; intX; "): "; astrSplitItems(intX)
Next
Print "Filtered Items (using '"; strFilterText; "'):"
astrFilteredItems = Filter(astrSplitItems, strFilterText, True, vbTextCompare)
For intX = 0 To UBound(astrFilteredItems)
Print "Item("; intX; "): "; astrFilteredItems(intX)
Next
strFilteredString = Join(astrFilteredItems, ",")
Print "Filtered Output String: "; strFilteredString
End Sub
When the program is run, respond to the first prompt as follows:
Respond to the second prompt as follows:
The following output results:
Let us analyze the "Try It" code to explain how this works.
First, the necessary variables are declared. Note that the presence of an empty pair of parentheses following "astrSplitItems" and "astrFilteredItems" declares these items as dynamic arrays:
Dim strInputString As String
Dim strFilterText As String
Dim astrSplitItems() As String
Dim astrFilteredItems() As String
Dim strFilteredString As String
Dim intX As Integer
Next, we prompt for our input data. The line
strInputString = InputBox("Enter a comma-delimited string of items:", _
"String Array Functions")
causes the comma-delimited string we entered ("Abby,Bubba,Charlie,Debbie,Edgar") to be stored in the variable "strInputString".
The line
strFilterText = InputBox("Enter Filter:", "String Array Functions")
caused the "bb" filter we entered to be stored in the variable "strFilterText".
Next, we simply print out the string that was input:
Print "Original Input String: "; strInputString
Then it gets interesting in the next segment:
Print "Split Items:"
astrSplitItems = Split(strInputString, ",")
For intX = 0 To UBound(astrSplitItems)
Print "Item("; intX; "): "; astrSplitItems(intX)
Next
In the segment above, the line
astrSplitItems = Split(strInputString, ",")
causes the five names we entered ("Abby,Bubba,Charlie,Debbie,Edgar") to be stored in separate elements of the "astrSplitItems" dynamic array, indexed from 0 to 4 (i.e., astrSplitItems(0) will contain "Abby" while astrSplitItems(4) will contain "Edgar").
The For/Next loop in the segment displays the array contents so we can verify the results of the Split function:
For intX = 0 To UBound(astrSplitItems)
Print "Item("; intX; "): "; astrSplitItems(intX)
Next
The filtering occurs in the next segment:
Print "Filtered Items (using '"; strFilterText; "'):"
astrFilteredItems = Filter(astrSplitItems, strFilterText, True, vbTextCompare)
For intX = 0 To UBound(astrFilteredItems)
Print "Item("; intX; "): "; astrFilteredItems(intX)
Next
In the segment above, the line
astrFilteredItems = Filter(astrSplitItems, strFilterText, True, vbTextCompare)
tells the Filter function to take the array of five names (astrSplitItems), go thru and apply the filter criteria to it (the "bb" that is contained in the strFilterText variable), and place the results of the filtering in the "astrFilteredItems" dynamic array. In this particular case, three names matched the filter (Abby, Bubba, and Debbie), so those three names were stored in indexes 0 to 2 of the astrFilteredItems array.
The For/Next loop in the segment displays the filtered array contents so we can verify the results of the Filter function:
For intX = 0 To UBound(astrFilteredItems)
Print "Item("; intX; "): "; astrFilteredItems(intX)
Next
In the last lines of the "Try It" code, the line
strFilteredString = Join(astrFilteredItems, ",")
uses the Join function to create one string that is the result of concatenating all elements of the astrFilteredItems array, separating each item with a comma.
The line
Print "Filtered Output String: "; strFilteredString
shows the resulting "joined" string.
Download the VB project code for the example above here.