Liberty BASIC Help Online |
WORD$( s$, n )
WORD$( stringExpression, n [,string delimiter] )
Description:
This function returns the nth word in the string, string variable or string expression, stringExpression. The leading and trailing spaces are stripped from stringExpression and then by default it is broken down into 'words' at the remaining spaces inside. If n is less than 1 or greater than the number of words in stringExpression, then "" is returned. The string delimiter is optional. When it is not used, the space character is the delimiter.
Usage:
print word$("The quick brown fox jumped over the lazy dog", 5)
Produces:
jumped
And:
' display each word of sentence$ on its own line
sentence$ = "and many miles to go before I sleep."
token$ = "?"
while token$ <> ""
index = index + 1
token$ = word$(sentence$, index)
print token$
wend
Produces:
and
many
miles
to
go
before
I
sleep.
Using the optional string delimiter:
You can now specify the delimiter string of one or more characters, so optionally you can read comma delimited or other strings:
token$ = "*"
parseMe$ = "this,is,,a,test"
idx = 0
while token$<>""
idx = idx + 1
token$ = word$(parseMe$, idx, ",")
if token$ <> "" then print token$
wend
Also, notice the doubled up comma in the test string. This will be returned as a comma. This is useful for detecting empty delimited fields in a string. Try substituting the following lines:
parseMe$ = "thisarfisarfarfaarftest"
and:
token$ = word$(parseMe$, idx, "arf")
Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/