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

Produces:

  this
  is
  ,
  a
  test

Notice the doubled up comma in the parseMe$ string. This is considered an empty entry and so in this case the delimiter will be returned: "," This is useful for detecting empty delimited fields in a string.

Let's try changing the delimiter.  Substitute the following lines:

  parseMe$ = "thisarfisarfarfaarftest"

and:

  token$ = word$(parseMe$, idx, "arf")

This produces:

  this
  is
  arf
  a
  test