Main Menu

Split To Array On Word

Started by kevin, March 07, 2008, 05:13:30 AM

Previous topic - Next topic

kevin

 This PlayBASIC Function splits a source string into fragments and stores the result in an array.


PlayBASIC Code: [Select]
   ; Create a string with some tags in it.  These tags will be the points where it's cut into our output array
MyString$="This is some text
this is some more text
and this is more text
done"


; Dimension the Tokens$ to hold the split string fragments
Dim Tokens$(100)

; Call the SplitToarrayOn Word function
TokenCount=SplitToArrayOnWord(MyString$, "
",Tokens$(),1)

; Display the fragements found int he Tokens() array
Print TokenCount
For lp=1 to TokenCount
print Tokens$(lp)
next


; Show the screen and wait for a key press from the user
Sync
Waitkey
end



// Lazy mans split on WORD. :)


Function SplitToArrayOnWord(SrcString$,SplitOnTag$, me$(),OutputPosition)

// First Replace the split word/tag with a unique Chr code, in this case the ASC CHR 13 character
ThisString$=Replace$(SrcString$,SplitOnTag$,Chr$(13),1,true,false)

/// next split the String into an array using the unique chr code.
TokensFound=SplitToArray(ThisString$,Chr$(13),me$(),OutputPosition)

EndFunction TokensFound