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$, _
               StringToParse$, _
                   Delimiter$  _
           [,SaveDelimitFlag%])

 Parameters:

  • RetVal% Number of parsed elements that were copied to OneDIMArray$.
  • OneDIMArray$ One dimensional array to hold elements of the parsed StringToParse$. Note well that the array must be large enough to hold all the tokens of the parsed StringToParse$ Parsed substrings are stored starting at array element zero. Note well that the lower bound of the OneDIMArray$ subscripts can not be set using OPTION BASE.
  • StringToParse$ is a string to parse containing the delimited elements.
  • Delimiter$ is a string of one or more characters with each character being considered as a delimiter.
  • SaveDelimitFlag% [OPTIONAL] 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

    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.

Remarks :

When SPLIT is used, tokens, from a previous invocation of SPLIT, sometimes are retained if the previous invocation had more tokens than the next line parsed.

For example:

First line parsed:

Hello, how are you today?


 tokens =
 1 = Hello
 2 = how
 3 = are
 4 = you
 5 = today

Then the second line is read in from the source.

End Sub

Now the array from split will look like so:


 tokens =
 1 = End
 2 = Sub
 3 = are
 4 = you
 5 = today

If the token buffer array is declared global or static, then the array will have to be clear before each line is parsed. Alternatively, the return from split can be used to find how many tokens are returned from each invocation of SPLIT and the array buffer redimensioned accordingly.

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

Result:


 SPLIT returns the value of 12
 1
 2
 3

 4
 5
 6


 7
 8
 9

Example 2 :


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

 B$ = "Here is,,,"  & CHR$(34) & "New,Test" & CHR$(34) & ";Test!"

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

 PRINT "Sample String = ", B$
 PRINT "************************************************"
 PRINT I , " Eliminate all delimiters(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

Result:


 Sample String = Here is,,,"New,Test";Test!
 ***********************************************
  5 Eliminate all delimiters(default behavior)
 ***********************************************
 [ 0] LEN= 7 Cell =Here is
 [ 1] LEN= 0 Cell =
 [ 2] LEN= 0 Cell =
 [ 3] LEN= 8 Cell =New,Test
 [ 4] LEN= 5 Cell =Test!

 ***********************************************
  9 Retain all delimiters and all null fields
 ***********************************************
 [ 0] LEN= 7 Cell =Here is
 [ 1] LEN= 1 Cell =,
 [ 2] LEN= 0 Cell =
 [ 3] LEN= 1 Cell =,
 [ 4] LEN= 0 Cell =
 [ 5] LEN= 1 Cell =,
 [ 6] LEN= 10 Cell ="New,Test"
 [ 7] LEN= 1 Cell =;
 [ 8] LEN= 5 Cell =Test!

 ***********************************************
  3 Eliminate all delimiters and null fields
 ***********************************************
 [ 0] LEN= 7 Cell =Here is
 [ 1] LEN= 8 Cell =New,Test
 [ 2] LEN= 5 Cell =Test!

 ***********************************************
  7 Retain all delimiters excluding null fields
 ***********************************************
 [ 0] LEN= 7 Cell =Here is
 [ 1] LEN= 1 Cell =,
 [ 2] LEN= 1 Cell =,
 [ 3] LEN= 1 Cell =,
 [ 4] LEN= 10 Cell ="New,Test"
 [ 5] LEN= 1 Cell =;
 [ 6] LEN= 5 Cell =Test!

DSPLIT function

Purpose: DSPLIT parses a dynamic 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% = DSPLIT(OneDIMArray$, _
                StringToParse$, _
                    Delimiter$  _
            [,SaveDelimitFlag%])

 Parameters:

  • RetVal% Number of parsed elements that were copied to OneDIMArray$.
  • OneDIMArray$ One dimensional arrary to hold elements of parsed StringToParse$. Parsed substrings are stored starting at array element zero. Note well that the lower bound of the OneDIMArray$ subscripts can not be set using OPTION BASE.
  • StringToParse$ is a string to parse containing the delimited elements.
  • Delimiter$ is a string of one or more characters with each character being considered as a delimiter.
  • SaveDelimitFlag% [OPTIONAL] 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

    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.

Example:


 DIM i%, j%, A$
 GLOBAL DYNAMIC Buffer$[12]
 
 A$ = "1,2;3 ,4;5,6,  7,8;9"
 
 i% = DSPLIT(Buffer$, A$, ", ;")
 
 PRINT "DSPLIT returns the value of", i
 
 FOR j% = 0 TO i%-1
 PRINT Buffer$[j%]
 NEXT

Result:


 DSPLIT returns the value of 12
 1
 2
 3

 4
 5
 6


 7
 8
 9