String Array Functions
(Split, Join, and Filter)
Split, Join, and Filter form 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. These functions are summarized as follows:
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. Split is available both as a Microsoft.VisualBasic namespace function as well as a native .NET method.
|
Join |
Joins (concatenates) elements of an array into an output string. Join is available both as a Microsoft.VisualBasic namespace function as well as a native .NET method.
|
Filter |
Returns a zero-based array containing subset of a string array based on a specified filter criteria. This function is the least commonly used of the three, but can be useful in some circumstances. Filter is available only as a Microsoft.VisualBasic namespace function; there is no equivalent native .NET method. |
The Split, Join, and Filter functions are described in further detail below:
|
|
Microsoft.VisualBasic Namespace Function |
VB.NET Method |
|
Function / Method Name: |
Split |
Split |
|
Description: |
Splits a string into separate elements (substrings) based on a delimiter (such as a comma or space) and stores the resulting elements (substrings) in a zero-based, one-dimensional array. |
Splits a string into separate elements (substrings) based on a delimiter (such as a comma or space) and stores the resulting elements (substrings) in a zero-based, one-dimensional array. |
|
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. Maximum number of substrings to be returned; by default, all substrings are returned.
compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings (0 = case sensitive, 1 = case-insensitive).
NOTE: The CompareMethod enumeration can be specified for the compare argument: for 0 (case-sensitive), CompareMethod.Binary is used; for 1 (case-insensitive), CompareMethod.Text is used. Alternatively, the older "vb" constants vbBinaryCompare and vbTextCompare can be used for 0 and 1 respectively.
|
String.Split([delimiter[,count]])
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. Maximum number of substrings to be returned; by default, all substrings are returned.
|
|
Example: |
Dim astrNameParts() As String Dim strNameData As String = "John,J.,Doe" astrNameParts = Split(strNameData, ",") ' astrNameParts now contains 3 elements: ' astrNameParts(0) contains "John" ' astrNameParts(1) contains "J." ' astrNameParts(2) contains "Doe"
|
Dim astrNameParts() As String Dim strNameData As String _ = "John,J.,Doe" astrNameParts = strNameData.Split(",") ' astrNameParts now contains 3 elements: ' astrNameParts(0) contains "John" ' astrNameParts(1) contains "J." ' astrNameParts(2) contains "Doe"
|
|
|
Microsoft.VisualBasic Namespace Function |
VB.NET Method |
|
Function / Method Name: |
Join |
Join |
|
Description: |
Returns a string created by joining a number of substrings contained in an array. |
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.
|
String.Join(delimiter,list)
delimiter String character used to separate the substrings in the returned string.
list One-dimensional array containing substrings to be joined.
|
|
Example: |
Dim astrFruit() As String ReDim astrFruit(2) astrFruit(0) = "apple" astrFruit(1) = "peach" astrFruit(2) = "pear" Dim strAllFruit As String strAllFruit = Join(astrFruit, "|") ' strAllFruit now contains "apple|peach|pear"
|
Dim astrFruit() As String ReDim astrFruit(2) astrFruit(0) = "apple" astrFruit(1) = "peach" astrFruit(2) = "pear" Dim strAllFruit As String strAllFruit = String.Join("|", astrFruit) ' strAllFruit now contains "apple|peach|pear"
|
|
Function Name: |
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).
NOTE: The CompareMethod enumeration can be specified for the compare argument: for 0 (case-sensitive), CompareMethod.Binary is used; for 1 (case-insensitive), CompareMethod.Text is used. Alternatively, the older "vb" constants vbBinaryCompare and vbTextCompare can be used for 0 and 1 respectively.
|
|
Example: |
Dim astrFruit() As String ReDim astrFruit(2) Dim astrNewFruit() As String
astrFruit(0) = "apple" astrFruit(1) = "peach" astrFruit(2) = "pear"
astrNewFruit = Filter(astrFruit, "PEA", True, CompareMethod.Text)
' astrNewFruit now has two elements: ' astrNewFruit(0) = "peach" ' astrNewFruit(1) = "pear"
|
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:
Sub Main()
Dim strInputString As String
Dim strFilterText As String
Dim astrSplitItems() As String
Dim astrFilteredItems() As String
Dim strFilteredString As String
Dim intX As Integer
Console.Write("Enter a comma-delimited string of items: ")
strInputString = Console.ReadLine()
Console.Write("Enter Filter: ")
strFilterText = Console.ReadLine()
Console.WriteLine("Original Input String: " & strInputString)
Console.WriteLine()
Console.WriteLine("Split Items:")
astrSplitItems = strInputString.Split(",")
For intX = 0 To UBound(astrSplitItems)
Console.WriteLine("Item(" & CStr(intX) & "): " & astrSplitItems(intX))
Next
Console.WriteLine()
Console.WriteLine("Filtered Items (using '" & strFilterText & "'):")
astrFilteredItems = Filter(astrSplitItems, strFilterText, True, CompareMethod.Text)
For intX = 0 To UBound(astrFilteredItems)
Console.WriteLine("Item(" & CStr(intX) & "): " & astrFilteredItems(intX))
Next
Console.WriteLine()
strFilteredString = String.Join(",", astrFilteredItems)
Console.WriteLine("Filtered Output String: " & strFilteredString)
Console.WriteLine("")
Console.WriteLine("(Press Enter to close this window.)")
Console.ReadLine()
End Sub
Screen shot of run:

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
Console.Write("Enter a comma-delimited string of items: ")
strInputString = Console.ReadLine()
causes the comma-delimited string we entered ("Abby,Bubba,Charlie,Debbie,Edgar") to be stored in the variable "strInputString".
The lines:
Console.Write("Enter Filter: ")
strFilterText = Console.ReadLine()
caused the "bb" filter we entered to be stored in the variable "strFilterText".
Next, we simply print out the string that was input:
Console.WriteLine("Original Input String: " & strInputString)
Console.WriteLine()
Then it gets interesting in the next segment:
Console.WriteLine("Split Items:")
astrSplitItems = strInputString.Split(",")
For intX = 0 To UBound(astrSplitItems)
Console.WriteLine("Item(" & CStr(intX) & "): " & astrSplitItems(intX))
Next
Console.WriteLine()
In the segment above, the line
astrSplitItems = strInputString.Split(",")
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)
Console.WriteLine("Item(" & CStr(intX) & "): " & astrSplitItems(intX))
Next
The filtering occurs in the next segment:
Console.WriteLine("Filtered Items (using '" & strFilterText & "'):")
astrFilteredItems = Filter(astrSplitItems, strFilterText, True, CompareMethod.Text)
For intX = 0 To UBound(astrFilteredItems)
Console.WriteLine("Item(" & CStr(intX) & "): " & astrFilteredItems(intX))
Next
Console.WriteLine()
In the segment above, the line
astrFilteredItems = Filter(astrSplitItems, strFilterText, True, CompareMethod.Text)
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)
Console.WriteLine("Item(" & CStr(intX) & "): " & astrFilteredItems(intX))
Next
In the last lines of the "Try It" code, the line
strFilteredString = String.Join(",", astrFilteredItems)
Console.WriteLine("Filtered Output String: " & strFilteredString)
uses the Join method to create one string that is the result of concatenating all elements of the astrFilteredItems array, separating each item with a comma.
The line
Console.WriteLine("Filtered Output String: " & strFilteredString)
shows the resulting "joined" string.
Download the VB project code for the example above here.