Visual Basic 6 function "explode"

Go back

Below you'll find the source for the Visual Basic 6 function explode.

Attribute VB_Name = "modExplode"
' This function is downloaded from:
' You may freely distribute this file but please leave all comments, including this one, in it.
' @Author Stefan Thoolen <>

Option Explicit

' Splits a string into an array with a delimiter char
' Same syntax as the PHP function 'explode'
' See also:
' @param    String  delimiter       The delimiter
' @param    Variant tstr            The text which must be exploded
' @param    Integer limit           The maximum number of parts (0 is infinite)
' @return   Variant                 All parts as an array
' @author   Stefan Thoolen <>
Public Function explode(delimiter As String, tstr As Variant, Optional limit As Integer) As Variant
    ' I wrote this function but later I found split() in VB6
    ' For compatibility reasons I kept this as a function wrapper
    ' Since I used other variable types, I converted them
    Dim p() As String, retval() As Variant, i As Integer
    If limit = 0 Then limit = -1
    p = Split(CStr(tstr), delimiter, CLng(limit), vbTextCompare)
    ReDim retval(LBound(p) To UBound(p))
    For i = LBound(p) To UBound(p)
        retval(i) = p(i)
    Next i
    explode = p
End Function