Anyone got a function to parse a string into component words?

Previous topic - Next topic

Cliff3D

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!)

Moru

I believe SPLITSTR() is made just for that :-)
(just don't put any chr$(0) in the string)

jaywat

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!

Moru

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.

Cliff3D

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!