Written By TheVBProgramer.
NOTE: This tutorial uses The VB Test Harness. If you have not already created this test harness please do so first. It will make this tutorial a lot easier to follow.
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.
Comments
Help.!
Private Sub Command1_Click()
Dim Total_Time As Integer
Dim NumberOf_Tracks As Integer
Dim Track_Length(20) As Integer
Dim Track_Title(20) As String
'Step 1'
Total_Time = 0
'Step 2'
Do
NumberOf_Tracks = InputBox("Please enter Number of Tracks.")
If NumberOf_Tracks > 20 Then
MsgBox ("Too Many Tracks.")
End If
Loop Until NumberOf_Tracks >= 1 And NumberOf_Tracks <= 20
'Step 3'
For Counter = 1 To NumberOf_Tracks
'Step 4'
Track_Title(20) = InputBox("Enter Track Title.")
Track_Length(20) = InputBox("Enter Track Length (Seconds).")
'Step 5'
Total_Time = Total_Time + Track_Length(20)
'Step 6'
Next Counter
'Step 7'
For Counter = 1 To NumberOf_Tracks
Form1.Print Track_Title(20); Track_Length(20); ("Seconds")
Next Counter
'Step 8'
Form1.Print ("CD-R Running Time -"); Total_Time
Can anyone help me display the Array's properly. Please help me with the Array's. :{
Hey, I have a huge string
Hey, I have a huge string seperated by several ",". So far no problem with the split function. But now I only want to perform a split after every second ",". Anyone an idea how that is possible?
Well Done !!
asf Simply the best. Good explanaiton with example.
Code doesn't work. Great!
Code doesn't work. Great!
Can We pass Array variable as Parameter for Select Case?
ProdName =Split(X,"-") 'some thing with split
Select case ProdName(0) 'Can we pass like this
Case 872999,874999,875199,875299,875699,875899,879699,880799,885299,886299,886499,889499,892599
amount1= GetXMLValue(strFileName, "paymentAmount", "amount","1")
msgbox amount1
Case "CDIT","LCDXTranche","cds999","cds99901"
amount1 =GetXMLValue(strFileName,"paymentAmount","amount","1")
msgbox amount1
Case "cmbx"
amount1 =GetXMLValue(strFileName,"paymentAmount","amount","2")
msgbox amount1
End Select
Im getting "type mismatch error" if ProdName(0) is 'String' ,But it works fine if ProdName(0) is 'Number' ie first case passes.Can you please suggest.
Thanks Guys !
This is great. I have been struggling to get this split function to work on my project for days and I am so happy that I found this site. It has been very helpful.
Keep on my the good work. It's just so great to have access to this kind of material while studying.
Cheers!
visual basic
plz............... help me in the program
function query
Split(Item, " , ", -1, 1)
Please Explain me the above instruction and what is the use of -1,1 co-ordinates in above instruction.
This is very helpfull but
This is very helpfull but what if I want to separate each charecter of a word into an array using the split function? Can anyone help me with that, I have been trying to do this for days now and I cant get a way of doing it. But I am using visual basic.net.
eg I want to separate the charecters of word "HELLO" into an array that would look like this:
array(0)="H"
array(1)="E"
array(2)="L"
array(3)="L"
array(4)="O"
Please help me to get this right...
Thanks
Use Mid string function
'use mid$ function
sString = "HELLO"
for iCnt = 1 to len(sString)
array(iCnt) = mid(sString,iCnt,1)
next
'Something like that.
Gr8
Gr8
Well trough your example i
Well trough your example i can manage to find my solution, but im still confused.
Im trying to split every word that an Input Box will have, but i need to save all the words into Array variables, i'm creating a Tag Cloud in Visual Basic 6.0.
Can you help me out with that? Cant figure out how to use Split function
vb6
can you just send me a vb6 that operate a selection of "menu" then the payment received, item to be paid, and change of the costumer...
please i need this as my project...
euricka!
this page satisfies my need, this' what i am looking for!
i think, not so soon my grades will be completed!
thanks!
Thanx
Just spent 1 hr making a splitter when I stumble upon this post telling me that it is already included....
*sigh*
very good
very good
Nice
Nice
Great
Great
Post new comment