===SPLIT Function (VB work-a-like)===
The Split() function cuts a string at specific points, returning the resultant sub-strings in an array and returns the highest dimension of the array.
The elements of the array are the delimited sub-strings of the input string. e.g. x=Split("fred,jim,sheila",",")
will break the string at each ","
return x=3 and the three sub-strings "fred"
, "jim"
& "sheila"
in elements 1,2 & 3 of the string array.
===Behaviour:===
SP$
. It is not currently possible to specify another Array.SP$(1)
as the entire string."jim"
as the delimiter would return two elements "fred,"
and ",sheila"
. z=Split(Time$,":")
FOR n=1 TO z
PRINT n,SP$(n)
NEXT
**The Code**:
Function Split(a$,b$) As Integer' returns the number of dimensions in SP$ always starts from 1 regardless of OPTION BASE
Local Integer z,n,m
If b$="" Then Split=0:Exit Function ' can't split with an empty delimiter
' if SP$ doesn't exist, the ERASE will cause an error, choose which ON ERROR SKIP you need
'MM.Ver <5.04
On Error Skip
'MM.Ver >=5.04
On Error Skip 1
Erase SP$
z=1:n=0
Do 'count instances of delimiter for DIM SP$()
z=Instr(z,a$,b$)
If z=0 Then
If n=0 Then ' no delimeters
Dim SP$(1):SP$(1)=a$:Split=1:Exit Function ' only one substring
Else
Exit Do
End If
Else
n=n+1:z=z+Len(b$)
End If
Loop
m=n+1:n=1
Dim SP$(m)
Do
z=Instr(1,a$,b$)
If z=0 Then
SP$(m)=a$:Exit Do
Else
SP$(n)=Left$(a$,z-1):a$=Mid$(a$,z+Len(b$)):n=n+1
End If
Loop
Split=m
End Function
===See also===
The [[Field_Function_cField|cField$]] CFunction extracts individual fields from a string in real-time rather than splitting and you might consider this as it offers advantages of speed and memory over Split() but has some limitations in that an empty string is returned if the delimiter is not found and there is no way of telling the number of substrings.
[[UBOUND_Upper_boundary_of_an_Array|UBound() Function]]\\