SPLIT function

Purpose:

SPLIT parses a string separated by delimiters, copies the parsed elements to an array and returns the number of parsed elements that were copied to the array. Parsed strings are stored starting at array element zero.

Syntax:

RetVal = SPLIT(OneDIMArray AS STRING, _
             StringToParse AS STRING, _
                 Delimiter AS STRING _
         [,SaveDelimitFlag AS INTEGER])

Return Value:

  • Data type: INTEGER
    RetVal The number of parsed elements that were copied to OneDIMArray.

Parameters:

  • Data type: STRING
    OneDIMArray A one dimensional array to hold elements of the split StringToParse.
  • Data type: STRING
    StringToParse The string to split.
  • Data type: STRING
    Delimiter One or more characters, each character considered a delimiter.
  • Data type: INTEGER
    SaveDelimitFlag [OPTIONAL] A flag to save delimiters to the array.
    • 0 = Eliminate all delimiters (default behavior)
    • 1 = Retain all delimiters and all null fields
    • 2 = Eliminate all delimiters and null fields
    • 3 = Retain all delimiters excluding null fields
    • 4 = Eliminate all delimiters retain delimited quotes

Remarks:

With SaveDelimitFlag set to default mode, 0, SPLIT strips quotation marks from quoted fields and is VB6 compatible handling null fields. When using options 1 or 3, in addition to the delimiters being included into the users parse stack, quoted strings stay quoted.

The OneDIMArray array must be sized large enough to hold all the elements of the parsed StringToParse.

Parsed substrings are stored starting at array element zero. The lower bound of the array cannot be set using OPTION BASE.

👉 Sometimes elements from a previous invocation of SPLIT are retained in the array if the current invocation of SPLIT has fewer elements than the previous invocation.

For example:

 DIM j%, A$, Buffer$[12]

 A$ = "1,2;3 ,4;5,6,  7,8;9"

 SPLIT(Buffer$, A$, ", ;")

 FOR j% = 0 TO 11
  PRINT Buffer$[j%]
 NEXT

 PRINT

 A$ = "A,B;C"

 SPLIT(Buffer$, A$, ", ;")

 FOR j% = 0 TO 11
  PRINT Buffer$[j%]
 NEXT

PAUSE

The first line,

1,2;3 ,4;5,6,  7,8;9

SPLIT, when parsed, contains 12 elements in the Buffer$ array, nine digits and three spaces.

1
2
3

4
5
6


7
8
9

Then the second line is SPLIT,

A,B;C

and now the Buffer$ array contains the three alpha chars from the second invocation replacing the previous invocation's first three elements in the Buffer$ array. However the remainder of the Buffer$ array still contains the elements from the previous invocation of SPLIT.

A
B
C

4
5
6


7
8
9

If the Buffer$ array is declared global or static, then to avoid retention of the elements from the previous invocation, the array will have to be cleared before each line is parsed.

Alternatively, the value returned from SPLIT can be used to find how many elements have been parsed and the program adjusted to process only the elements from the latest invocation of SPLIT.

Example 1:

DIM i%, j%, A$, Buffer$[12]

A$ = "1,2;3 ,4;5,6,  7,8;9"

i% = SPLIT(Buffer$, A$, ", ;")

PRINT "SPLIT returns the value of", i

FOR j% = 0 TO i%-1
  PRINT Buffer$[j%]
NEXT

PRINT

A$ = "A,B;C"

i% = SPLIT(Buffer$, A$, ", ;")

PRINT "SPLIT returns the value of", i

FOR j% = 0 TO i%-1
  PRINT Buffer$[j%]
NEXT

PAUSE

Result:

SPLIT returns the value of 12
1
2
3

4
5
6


7
8
9

SPLIT returns the value of 3
A
B
C

Example 2:

CLS
DIM A$[100], B$, I, J

B$ = " This;,,     is,just an,example;" & CHR$(34) & "of " & CHR$(34) & CHR$(34) _
& ",one" & CHR$(34) & CHR$(34) & " way" & CHR$(34) & ",to do it"

I = SPLIT(A$, B$, ",;+", 0)

PRINT " Sample String = ", B$
PRINT
PRINT " ***********************************************************"
PRINT I , "  Eliminate all delimiters and quotes (default behavior)"
PRINT " ***********************************************************"

FOR J = 0 TO I-1
 PRINT " [",J,"] LEN = ", LEN(A$[J])," Cell = " , A$[J]
NEXT

PRINT
I = SPLIT(A$, B$, ",;+", 1)

PRINT " ************************************************"
PRINT I , "  Retain all delimiters and all null fields"
PRINT " ************************************************"

FOR J = 0 TO I-1
 PRINT " [",J,"] LEN =", LEN(A$[J])," Cell = " , A$[J]
NEXT

PRINT
I = SPLIT(A$, B$, ",;+", 2)

PRINT " ************************************************"
PRINT I , "  Eliminate all delimiters and null fields"
PRINT " ************************************************"

FOR J = 0 TO I-1
 PRINT " [",J,"] LEN = ", LEN(A$[J])," Cell = " , A$[J]
NEXT

PRINT
I = SPLIT(A$, B$, ",;+", 3)

PRINT " ************************************************"
PRINT I , "  Retain all delimiters excluding null fields"
PRINT " ************************************************"

FOR J = 0 TO I-1
 PRINT " [",J,"] LEN = ", LEN(A$[J])," Cell = " , A$[J]
NEXT

PRINT
I = SPLIT(A$, B$, ",;+", 4)

PRINT " *****************************************************"
PRINT I , "  Eliminate all delimiters retain delimited quotes"
PRINT " *****************************************************"

FOR J = 0 TO I-1
 PRINT " [",J,"] LEN = ", LEN(A$[J])," Cell = " , A$[J]
NEXT

PAUSE

Result:

Sample String =  This;,,     is,just an,example;"of "",one"" way",to do it

***********************************************************
8  Eliminate all delimiters and quotes (default behavior)
***********************************************************
[ 0] LEN =  5 Cell =  This
[ 1] LEN =  0 Cell =
[ 2] LEN =  0 Cell =
[ 3] LEN =  7 Cell =      is
[ 4] LEN =  7 Cell = just an
[ 5] LEN =  7 Cell = example
[ 6] LEN =  11 Cell = of ,one way
[ 7] LEN =  8 Cell = to do it

************************************************
15  Retain all delimiters and all null fields
************************************************
[ 0] LEN = 5 Cell =  This
[ 1] LEN = 1 Cell = ;
[ 2] LEN = 0 Cell =
[ 3] LEN = 1 Cell = ,
[ 4] LEN = 0 Cell =
[ 5] LEN = 1 Cell = ,
[ 6] LEN = 7 Cell =      is
[ 7] LEN = 1 Cell = ,
[ 8] LEN = 7 Cell = just an
[ 9] LEN = 1 Cell = ,
[ 10] LEN = 7 Cell = example
[ 11] LEN = 1 Cell = ;
[ 12] LEN = 17 Cell = "of "",one"" way"
[ 13] LEN = 1 Cell = ,
[ 14] LEN = 8 Cell = to do it

************************************************
6  Eliminate all delimiters and null fields
************************************************
[ 0] LEN =  5 Cell =  This
[ 1] LEN =  7 Cell =      is
[ 2] LEN =  7 Cell = just an
[ 3] LEN =  7 Cell = example
[ 4] LEN =  11 Cell = of ,one way
[ 5] LEN =  8 Cell = to do it

************************************************
13  Retain all delimiters excluding null fields
************************************************
[ 0] LEN =  5 Cell =  This
[ 1] LEN =  1 Cell = ;
[ 2] LEN =  1 Cell = ,
[ 3] LEN =  1 Cell = ,
[ 4] LEN =  7 Cell =      is
[ 5] LEN =  1 Cell = ,
[ 6] LEN =  7 Cell = just an
[ 7] LEN =  1 Cell = ,
[ 8] LEN =  7 Cell = example
[ 9] LEN =  1 Cell = ;
[ 10] LEN =  17 Cell = "of "",one"" way"
[ 11] LEN =  1 Cell = ,
[ 12] LEN =  8 Cell = to do it

*****************************************************
8  Eliminate all delimiters retain delimited quotes
*****************************************************
[ 0] LEN =  5 Cell =  This
[ 1] LEN =  0 Cell =
[ 2] LEN =  0 Cell =
[ 3] LEN =  7 Cell =      is
[ 4] LEN =  7 Cell = just an
[ 5] LEN =  7 Cell = example
[ 6] LEN =  13 Cell = of ",one" way
[ 7] LEN =  8 Cell = to do it

DSPLIT function

Purpose:

DSPLIT parses a string separated by delimiters, copies the parsed elements to a DYNAMIC array and returns the number of parsed elements that were copied to the array. Parsed strings are stored starting at array element zero.

Syntax:

RetVal = DSPLIT(OneDIMArray AS STRING, _
              StringToParse AS STRING, _
                  Delimiter AS STRING _
          [,SaveDelimitFlag AS INTEGER])

Return Value:

  • Data type: INTEGER
    RetVal The number of parsed elements copied to OneDIMArray.

Parameters:

  • Data type: STRING
    OneDIMArray One dimensional DYNAMIC array to hold elements of the split StringToParse.
  • Data type: STRING
    StringToParse The string to split.
  • Data type: STRING
    Delimiter One or more characters, each character considered a delimiter.
  • Data type: INTEGER
    SaveDelimitFlag [OPTIONAL] A flag to save delimiters to the array.
    • 0 = Eliminate all delimiters(default behavior)
    • 1 = Retain all delimiters and all null fields
    • 2 = Eliminate all delimiters and null fields
    • 3 = Retain all delimiters excluding null fields
    • 4 = Eliminate all delimiters retain delimited quotes

Remarks:

With SaveDelimitFlag set to default mode, 0, DSPLIT strips quotation marks from quoted fields and is VB6 compatible handling null fields. When using options 1 or 3, in addition to the delimiters being included into the users parse stack, quoted strings stay quoted.

The array must be sized large enough to hold all the tokens of the split StringToParse.

Parsed substrings are stored starting at array element zero. The lower bound of the array can not be set using OPTION BASE.

Example:

DIM Sentence$, TheSplit$, Elements%, Delimiters%, FieldCount%, ArraySize%

Sentence$ = " This;,,     is,just an,example;" & CHR$(34) & "of " & CHR$(34) & CHR$(34) _
& ",one" & CHR$(34) & CHR$(34) & " way" & CHR$(34) & ",to do it"

?
PRINT " Sample String = ", Sentence$
?
TheSplit$ = " Split on " & DQ$ & ";" & DQ$ & " and " & DQ$ & "," & DQ$
PRINT TheSplit$
?
Delimiters% = TALLY(Sentence$,";") + TALLY(Sentence$,",")
PRINT " The Delimiters total is", Delimiters%
?
FieldCount% = Delimiters% + 1

ArraySize% = FieldCount% + Delimiters%

PRINT " The ArraySize needed is", ArraySize%

DIM DYNAMIC Buffer$[ArraySize]

?
PRINT " ***********************************************************"
PRINT "  Eliminate all delimiters"
PRINT " ***********************************************************"
?
Elements% = DSPLIT(Buffer$, Sentence$, ";,") ' split on ";" and "," 
PRINT " The number of Elements in the parsed string is", Elements%
?
FOR INTEGER i = 0 TO Elements% - 1
 PRINT " Element ", i%, ": ", Buffer$[i] 'see how we did 
NEXT
?
PRINT " ***********************************************************"
PRINT "  Retain all delimiters and all null fields"
PRINT " ***********************************************************"
?
Elements% = DSPLIT(Buffer$, Sentence$, ";,", 1) ' split on ";" and "," 
PRINT " The number of Elements in the parsed string is", Elements%
?
FOR INTEGER i = 0 TO Elements% - 1
PRINT " Element ", i%, ": ", Buffer$[i] 'see how we did 
NEXT
 ?
PRINT " ***********************************************************"
PRINT "  Eliminate all delimiters and null fields "
PRINT " ***********************************************************"
?
Elements% = DSPLIT(Buffer$, Sentence$, ";,", 2) ' split on ";" and "," 
PRINT " The number of Elements in the parsed string is", Elements%
?
FOR INTEGER i = 0 TO Elements% - 1
 PRINT " Element ", i%, ": ", Buffer$[i] 'see how we did 
NEXT
?
PRINT " ***********************************************************"
PRINT "  Retain all delimiters eliminate null fields"
PRINT " ***********************************************************"
?
Elements% = DSPLIT(Buffer$, Sentence$, ";,", 3) ' split on ";" and "," 
PRINT " The number of Elements in the parsed string is", Elements%
?
FOR INTEGER i = 0 TO Elements% - 1
 PRINT " Element ", i%, ": ", Buffer$[i] 'see how we did 
NEXT
 ?
PRINT " ***********************************************************"
PRINT "  Eliminate all delimiters retain delimited quotes"
PRINT " ***********************************************************"
?
Elements% = DSPLIT(Buffer$, Sentence$, ";,", 4) ' split on ";" and "," 
PRINT " The number of Elements in the parsed string is", Elements%
?
FOR INTEGER i = 0 TO Elements% - 1
 PRINT " Element ", i%, ": ", Buffer$[i] 'see how we did 
NEXT

FREE Buffer$

PAUSE

Result:

Sample String =  This;,,     is,just an,example;"of "",one"" way",to do it

Split on ";" and ","

The Delimiters total is 8

The ArraySize needed is 17

***********************************************************
 Eliminate all delimiters
***********************************************************

The number of Elements in the parsed string is 8

Element  0:  This
Element  1:
Element  2:
Element  3:      is
Element  4: just an
Element  5: example
Element  6: of ,one way
Element  7: to do it

***********************************************************
 Retain all delimiters and all null fields
***********************************************************

The number of Elements in the parsed string is 15

Element  0:  This
Element  1: ;
Element  2:
Element  3: ,
Element  4:
Element  5: ,
Element  6:      is
Element  7: ,
Element  8: just an
Element  9: ,
Element  10: example
Element  11: ;
Element  12: "of "",one"" way"
Element  13: ,
Element  14: to do it

***********************************************************
 Eliminate all delimiters and null fields
***********************************************************

The number of Elements in the parsed string is 6

Element  0:  This
Element  1:      is
Element  2: just an
Element  3: example
Element  4: of ,one way
Element  5: to do it

***********************************************************
 Retain all delimiters eliminate null fields
***********************************************************

The number of Elements in the parsed string is 13

Element  0:  This
Element  1: ;
Element  2: ,
Element  3: ,
Element  4:      is
Element  5: ,
Element  6: just an
Element  7: ,
Element  8: example
Element  9: ;
Element  10: "of "",one"" way"
Element  11: ,
Element  12: to do it

***********************************************************
 Eliminate all delimiters retain delimited quotes
***********************************************************

The number of Elements in the parsed string is 8

Element  0:  This
Element  1:
Element  2:
Element  3:      is
Element  4: just an
Element  5: example
Element  6: of ",one" way
Element  7: to do it

Press any key to continue . . .