SplitToArray
Tokens = SplitToArray(SourceString$, DelimiterChrs$, OuputArray(), [OutputIndex=0], [Flags=7])
 
Parameters:

    SourceString$ = The String you wish to break up
    DelimiterChrs$ = The characters that should be used as a delimiters (seperators)
    OuputArray() = The array that you wish to store the tokens in.
    [OutputIndex=0] = The starting position of where the tokens should be placed, defaults to 0
    [Flags=7] = Flags control how the string is to be broken up
Returns:

    Tokens = The number of tokens returned
 

      SplitToArray will cut a string up into what are called 'tokens' placed them into an array. This allows us to easily break up delimited strings (Strings with common separator characters in them) so you can work upon the fragments.

      SplitToArray is powerful enough to not only store the strings segments into a string arrays, but it can also convert the strings into integer or floating point arrays automatically for you. So it's perfect for dealing lists of values.



     Flag Bits

      Flags control how SplitToArray breaks up the string. By combining the flag values together, to can alter how the function behaves. It defaults to 7 which has all mode active (1 +2 +4).

           1 = remove null strings
           2 = left trim
           4 = right trim





FACTS:


      * SplitToArray can only stores values in 1D Integer, Float or String arrays.

      * SplitToArray will expand the target array if need be. However it doesn't Redim the array when resizing, so any previous information will be lost if SplitToArray needs to expand the array for you.





Mini Tutorial #1:


      This example cuts a string of characters that are delimited using the ',' character into a string array. Notice how the string array is expanded to fit the new data.


  
; Dimension an array called MyString$()
  Dim MyString$(1)
  
; Create a string variable s$ and assign it a string
  s$="A,B,C,D"
  
; Split the S$ into Mystring$()
  Tokens=SplitToArray(s$,",",MyString$(),1)
  
; Display the number of tokens found
  Print "Number of tokens found:"+Str$(Tokens)
  
; Display each token
  For lp=1 To Tokens
     Print MyString$(lp)
  Next
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  


This example would output.

  
  Number of tokens found:4
  A
  B
  C
  D
  





Mini Tutorial #2:


      This example cuts a string of characters using a collection of delimiters using the ',' character into a string array. Notice how the string array is expanded to fit the new data.


  
; Dimension an array called MyString$()
  Dim MyString$(1)
  
; Create a string variable s$ and assign it a string
  s$="Stuff=100,200,300,400"
  
; Split the S$ into Mystring$()
  Tokens=SplitToArray(s$,"=,",MyString$(),1)
  
; Display the number of tokens found
  Print "Number of tokens found:"+Str$(Tokens)
  
; Display each token
  For lp=1 To Tokens
     Print MyString$(lp)
  Next
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  


This example would output.

  
  Number of tokens found:5
  Stuff
  100
  200
  300
  400
  





Mini Tutorial #3:


      SplitToArray has This example cuts a string of characters using a collection of delimiters using the ',' character into a string array. Notice how the string array is expanded to fit the new data.


  
  
  a$="asas ,, bbb ,  "
  Dim Stuff$(0)
  ShowIt(a$,0)  ; allow null length strings and no trims
  ShowIt(a$,1+2+4)     ; Default
  ShowIt(a$,2)  ; Left trim + allow null length strings
  ShowIt(a$,4)  ; right trim + allow null length strings
  ShowIt(a$,1+2)  ; Left trim + allow null length strings
  ShowIt(a$,1+4)  ; right trim + allow null length strings
  
  Sync
  WaitKey
  
  
Function  ShowIt(s$,Flags)
  Print "------------------------------"
  Print "Flags"+Str$(Flags)
  Count=SplitToArray(s$,",",Stuff$(),0,Flags)
  Print "Count:"+Str$(COunt)
  For lp=0 To Count-1
     Print ">>>"+Stuff$(lp)+"<<<"
  Next
  
EndFunction
  
  



 
Related Info: Dim | Instring | Left$ | Mid$ | Right$ :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com