GLBasic forum

Main forum => GLBasic - en => Topic started by: Cliff3D on 2010-Sep-02

Title: Anyone got a function to parse a string into component words?
Post by: Cliff3D on 2010-Sep-02
As above, does anybody have a function to split a string into an array of strings containing the individual words? If not, that's probably my next task (but I'd be happy to use some existing tried-and-tested code!)
Title: Re: Anyone got a function to parse a string into component words?
Post by: Moru on 2010-Sep-02
I believe SPLITSTR() is made just for that :-)
(just don't put any chr$(0) in the string)
Title: Re: Anyone got a function to parse a string into component words?
Post by: jaywat on 2010-Sep-02
surely splitstr? just put any possible punctuation and a space as the seperators

e.g.

Code (glbasic) Select

sentence$ = "The quick brown fox jumped over the lazy dogs back. Adding some punctuation etc!, just to demonstrate..."

count = SPLITSTR(sentence$, words$[], " ,.!")

ypos = 0
FOR word = 0 TO LEN(words$[])-1
PRINT words$[word],0,ypos
INC ypos, 20
NEXT

SHOWSCREEN

KEYWAIT


edit: oh. beaten to it!
Title: Re: Anyone got a function to parse a string into component words?
Post by: Moru on 2010-Sep-02
Yes beat you to it but yours were better :-)

Splitstr() should ignore repetitive separators so you should end up with a nice clean array even though the sentence has "Hello...you" or similar.
Title: Re: Anyone got a function to parse a string into component words?
Post by: Cliff3D on 2010-Sep-02
Oooeer! That looks perfect, thanks - I don't recall SPLITSTR() but I guess it's been 10 years or so, either my memory could have just dropped a few bits or BASIC could have improved - many thanks, that should speed up todays project nicely!