README.md

December 5, 2022 ยท View on GitHub

VB6 Split Function in VB5

Description

This code duplicates the functionality of VB6's split function.

More Info

TheString$: The string to be parsed

Optional Delim:ThedilimetertoparseTheString: The dilimeter to parse TheString by

Optional Limit: The maximum number of elements to return.

If Delim is ommited or is not found, a single element array is returned.

To have Split return all of the substring, Limit should be set to -1.

Variant containing an array

If your program is migrated to VB6, this code will need to be removed, as it shares the same functionality (and name) of a VB6 intrisync function.

Submitted On
ByAgent153
LevelIntermediate
User Rating4.0 (8 globes from 2 users)
CompatibilityVB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0
CategoryString Manipulation
WorldVisual Basic
Archive File

Source Code

Function Split(TheString As String, Optional Delim As String, Optional Limit As Long = -1) As Variant
  'Duplicates the functionality of the vb6 counterpart.
  'Unfortunately, I was unable to include the vbcompare part of the vb6 funtionality.
  'Just use Option Campare at the beggining of this module.
  Dim dynArray() As Variant
  If Len(Delim) > 0 Then
    Dim ArrCt%
    Dim CurPos%
    Dim LenAssigned%
    Dim CurStrLen%
    ArrCt% = 0
    CurPos% = 1
    LenAssigned% = 1
    CurStrLen% = Len(TheString$)
    Do
      ReDim Preserve dynArray(0 To ArrCt%)
      CurStrLen% = (InStr(CurPos%, TheString$, Delim$) - CurPos%)
      If CurStrLen% < 0 Then
        dynArray(ArrCt%) = Right$(TheString$, (Len(TheString$) - (LenAssigned% - 1)))
        Exit Do
      Else
        dynArray(ArrCt%) = Mid$(TheString$, CurPos%, CurStrLen%)
      End If
      LenAssigned% = LenAssigned% + (Len(dynArray(ArrCt%)) + Len(Delim$))
      CurPos% = LenAssigned%
      ArrCt% = ArrCt% + 1
      If Not Limit = -1 Then
        If ArrCt = Limit Then Exit Do
      End If
    Loop
    Split = dynArray
  Else
    'duplicate the functionality more acuratley
    ReDim dynArray(0 To 0)
    dynArray(0) = TheString
    Split = dynArray
  End If
End Function