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:

Part

Description

expression

Required. String expression containing substrings and delimiters. If expression is a zero-length string(""), Split returns an empty array, that is, an array with no elements and no data.

delimiter

Optional. String character used to identify substring limits. If omitted, the space character (" ") is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.

count

Optional. Number of substrings to be returned; –1 (the default) indicates that all substrings are returned.

compare

Optional. Numeric value indicating the kind of comparison to use when evaluating substrings (0 = case sensitive, 1 = case-insensitive).

Built-in "vb" constants can be used for the compare argument:

vbBinaryCompare for 0 (case-sensitive search)

vbTextCompare for 1 (case-insensitive search)

 

 

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:

Part

Description

list

Required. One-dimensional array containing substrings to be joined.

delimiter

Optional. String character used to separate the substrings in the returned string. If omitted, the space character (" ") is used. If delimiter is a zero-length string (""), all items in the list are concatenated with no delimiters.

 

 

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:

Part

Description

InputStrings

Required. One-dimensional array of strings to be searched.

Value

Required. String to search for.

Include

Optional. Boolean value indicating whether to return substrings that include or exclude Value. If Include is True, Filter returns the subset of the array that contains Value as a substring. If Include is False, Filter returns the subset of the array that does not contain Value as a substring.

Compare

Optional. Numeric value indicating the kind of comparison to use (0 = case sensitive, 1 = case-insensitive).

Built-in "vb" constants can be used for the compare argument:

vbBinaryCompare for 0 (case-sensitive search)

vbTextCompare for 1 (case-insensitive search)

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

   

    Print "Split Items:"

    astrSplitItems = Split(strInputString, ",")

    For intX = 0 To UBound(astrSplitItems)

        Print "Item("; intX; "): "; astrSplitItems(intX)

    Next

    Print

   

    Print "Filtered Items (using '"; strFilterText; "'):"

   

    astrFilteredItems = Filter(astrSplitItems, strFilterText, True, vbTextCompare)

    For intX = 0 To UBound(astrFilteredItems)

        Print "Item("; intX; "): "; astrFilteredItems(intX)

    Next

    Print

   

    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

    Print

   

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

    Print

 

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

    Print

 

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.