Public Function IsGoodSplit(Optional iTypeSplit As Long) As Boolean
' verify correct Split returns, rev 20020601
' returns True if all tests are passed

  Dim fFailed As Boolean
  Dim asSplit() As String
  Dim avSplit As Variant
  Dim cntTokens As Long
  
  ' replace "SplitA01/SplitB01/SplitC01" with the name of your function to test
  Select Case iTypeSplit
  Case 0
    ' A version: returns (variant) array in function
    
    ' vanilla
    avSplit = SplitA01("a b")
    If vArr2sArr(avSplit, asSplit()) >= 0 Then
      If Joint(asSplit) <> "a,b" Then Stop: fFailed = True
    Else
      Stop: fFailed = True
    End If
    
    ' delimiters front and back
    avSplit = SplitA01(" a bc d ")
    If vArr2sArr(avSplit, asSplit()) >= 0 Then
      If Joint(asSplit) <> ",a,bc,d," Then Stop: fFailed = True
    Else
      Stop: fFailed = True
    End If
    
    ' longer delimiter
    avSplit = SplitA01("ab123cd123ef", "123")
    If vArr2sArr(avSplit, asSplit()) >= 0 Then
      If Joint(asSplit) <> "ab,cd,ef" Then Stop: fFailed = True
    Else
      Stop: fFailed = True
    End If
  
    ' no delimiters found
    avSplit = SplitA01("a")
    If vArr2sArr(avSplit, asSplit()) >= 0 Then
      If Joint(asSplit) <> "a" Then Stop: fFailed = True
    Else
      Stop: fFailed = True
    End If
    
    ' delimiter empty
    avSplit = SplitA01("a b", "")
    If vArr2sArr(avSplit, asSplit()) >= 0 Then
      If Joint(asSplit) <> "a b" Then Stop: fFailed = True
    Else
      Stop: fFailed = True
    End If
     
    ' expression empty
    ' note: VBA.Split returns unbound array, but for us a single-element
    '       array containing a zero-length string is good enough
    avSplit = SplitA01("")
    If UBound(avSplit) = -1 Then
      ' that's ok
    Else
      If vArr2sArr(avSplit, asSplit()) >= 0 Then
        If Joint(asSplit) <> "" Then Stop: fFailed = True
      Else
        Stop: fFailed = True
      End If
    End If
 
    ' count 2 (=> return no more than 2 elements)
    avSplit = SplitA01("a,b,c", ",", 2)
    ''asSplit() = VBA.Split("a,b,c", ",", 2)
    If vArr2sArr(avSplit, asSplit()) >= 0 Then
      If Joint(asSplit, "+") <> "a+b,c" Then Stop: fFailed = True
    Else
      Stop: fFailed = True
    End If
    
    ' count 0 (=> return no elements = unbound array/UBound=-1)
    ''asSplit() = VBA.Split("a,b,c", ",", 0)
    avSplit = SplitA01("a,b,c", ",", 0)
    If UBound(avSplit) <> -1 Then Stop: fFailed = True
  
    ' compare text
    avSplit = SplitA01("abc", "B", , vbTextCompare)
    If vArr2sArr(avSplit, asSplit()) >= 0 Then
      If Joint(asSplit) <> "a,c" Then Stop: fFailed = True
    Else
      Stop: fFailed = True
    End If
  
    ' 20020601: new tests
    ' delimiter only
    avSplit = SplitA01(",", ",")
    If vArr2sArr(avSplit, asSplit()) >= 0 Then
      If Joint(asSplit) <> "," Then Stop: fFailed = True
    Else
      Stop: fFailed = True
    End If
    
  
  Case 1
    ' B version: returns (string) array in argument
    
    ' vanilla
    Call SplitB05("a b", asSplit())
    If Joint(asSplit) <> "a,b" Then Stop: fFailed = True
    
    ' delimiters front and back
    Call SplitB05(" a bc d ", asSplit())
    If Joint(asSplit) <> ",a,bc,d," Then Stop: fFailed = True
  
    ' longer delimiter
    Call SplitB05("ab123cd123ef", asSplit(), "123")
    If Joint(asSplit) <> "ab,cd,ef" Then Stop: fFailed = True
  
    ' overlapping delimiter
    Call SplitB05("abaaacd", asSplit(), "aa")
    If Joint(asSplit) <> "ab,acd" Then Stop: fFailed = True
    
    ' no delimiters found
    Call SplitB05("a", asSplit())
    If Joint(asSplit) <> "a" Then Stop: fFailed = True
    
    ' delimiter ""
    Call SplitB05("a b", asSplit(), "")
    If Joint(asSplit) <> "a b" Then Stop: fFailed = True
    
    ' expression "" (=> single-element array containing a zero-length string)
    Call SplitB05("", asSplit())
    If Joint(asSplit) <> "" Then Stop: fFailed = True
    
    ' 20020601: new tests
    ' delimiter only
    Call SplitB05(",", asSplit(), ",")
    If Joint(asSplit) <> "," Then Stop: fFailed = True
    
  Case 2
    ' C version: returns (string) array in argument, function returns token count
    
    ' vanilla
    cntTokens = SplitC01("a b", asSplit())
    If cntTokens <> 2 Or Joint(asSplit) <> "a,b" Then Stop: fFailed = True
    
    ' delimiters front and back
    cntTokens = SplitC01(" a bc d ", asSplit())
    If cntTokens <> 5 Or Joint(asSplit) <> ",a,bc,d," Then Stop: fFailed = True
  
    ' longer delimiter
    cntTokens = SplitC01("ab123cd123ef", asSplit(), "123")
    If cntTokens <> 3 Or Joint(asSplit) <> "ab,cd,ef" Then Stop: fFailed = True
  
    ' overlapping delimiter
    cntTokens = SplitC01("abaaacd", asSplit(), "aa")
    If cntTokens <> 2 Or Joint(asSplit) <> "ab,acd" Then Stop: fFailed = True
    
    ' no delimiters found
    cntTokens = SplitC01("a", asSplit())
    If cntTokens <> 1 Or Joint(asSplit) <> "a" Then Stop: fFailed = True
    
    ' delimiter ""
    cntTokens = SplitC01("a b", asSplit(), "")
    If cntTokens <> 1 Or Joint(asSplit) <> "a b" Then Stop: fFailed = True
    
    ' expression "" (=> single-element array containing a zero-length string)
    cntTokens = SplitC01("", asSplit())
    If cntTokens <> 1 Or Joint(asSplit) <> "" Then Stop: fFailed = True
      
    ' 20020601: new tests
    ' delimiter only
    cntTokens = SplitC01(",", asSplit(), ",")
    If cntTokens <> 2 Or Joint(asSplit) <> "," Then Stop: fFailed = True
    
  End Select
  
  ' well done
  IsGoodSplit = Not fFailed
  
End Function

Public Property Get Joint(sArr() As String, Optional Delimiter As String = ",") As String
  ' simple Join emulation (Join02)
  Dim i As Long
  ' is array bound
  If Not Val(Not sArr) Then
    For i = LBound(sArr) To UBound(sArr) - 1
      Joint = Joint & (sArr(i) & Delimiter) ' add smaller parts first
    Next
    Joint = Joint & sArr(UBound(sArr))
  End If
End Property

Public Function vArr2sArr(vArray As Variant, sArrRet() As String) As Long
' variant array to string array
  Dim i As Long
  Erase sArrRet
  vArr2sArr = -1
  If IsArray(vArray) Then
    If UBound(vArray) >= 0 Then
      ReDim sArrRet(UBound(vArray))
      For i = 0 To UBound(vArray)
        sArrRet(i) = vArray(i)
      Next
      vArr2sArr = UBound(sArrRet)
    End If
  End If
End Function


Back to Split