May 20, 2019
Hot Topics:

My Favourite Functions - Part 3

  • November 20, 2002
  • By Karl Moore
  • Send Email »
  • More Articles »

VB6 Split Function (VB6 Only)

You can use the Split() function to parse out a string depending on a delimiter.

In other words, using Split(), the string "hello:my:friend" can be broken down into three separate parts and thrown into an array.

Here's an example of its usage:

Dim strTextArray() As StringstrTextArray() = Split("hello:my:friend", ":")MsgBox strTextArray(0)

The last message box displays the first element (0) of the array - "hello". The second and third elements will contain the "my" and "friend" values.

So to use the Split() function, follow this syntax:

ArrayToPutInto() = Split(DelimitedString, Delimiter)

Here are a couple of other functions that might interest readers. Look them up in the help if you're interested:

  • Join() - this function does the opposite of Split(). It takes an array and puts it all together into one big string, separated by a specified delimiter
  • Filter() - returns a subset of array information, dependant on criteria
  • -- MiniMonkey from Somewhere Touching the Big Pond.

    Non-VB6 Split Function

    One of my favourite functions in Visual Basic 6 is Split (see above). But when I moved to my new job in Cape Town, we moved down to version 5, which doesn't support it. So here's my own mini-version of Split:

    Private Sub Command1_Click()Dim x() As Variantx() = WordSplit("Hello:my:friend", ":")For i = LBound(x) To UBound(x)MsgBox x(i)Next iEnd SubPublic Function WordSplit(sWords As String, sDelim As String) As VariantDim sTemp As StringDim vWords() As VariantDim n, nCount As IntegernCount = 0sTemp = sWordsDon = InStr(1, sTemp, sDelim)ReDim Preserve vWords(nCount)If n = 0 Then  vWords(nCount) = sTempElse  vWords(nCount) = Left$(sTemp, n)  If Right(vWords(nCount), 1) = sDelim Then    vWords(nCount) = Mid(vWords(nCount), 1, _    Len(vWords(nCount)) - 1)  End If  sTemp = Mid$(sTemp, n + 1)End IfnCount = nCount + 1Loop Until n = 0WordSplit = vWordsEnd Function

    -- African Abdul, Cape Town.

    Page 4 of 5

    Comment and Contribute


    (Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date