Manual:DIL Manual/getwords()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:15, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

getwords()

Function (stringlist). Splits a string into its words.

getwords(s : string)

Returns a stringlist with one entry per word of s, in order. Words are separated by spaces; any number of spaces counts as one separator, and no empty entries are made. s itself is not changed (unlike getword()).

Parameters

Name Type Meaning
s string The text to split.

Returns

Stringlist: the words. Empty when s is "" or only spaces. It fails if s fails or is a string variable that has never had a value; assigning a failed list leaves the variable as it was.

Notes

  • Only the space character separates words. Tabs and line breaks stay inside the words: getwords("a\nb") is one entry. For lines, use split(text, "\n"), or split(text, "<br/>") for text kept as HTML.
  • length() gives the number of words; words.[0] is the first.

Example

dilbegin integer count_words(s : string);
var
   words : stringlist;
code
{
   words := getwords(s);
   return (length(words));
}
dilend

Counts the words in a string: " open the door " gives 3.

See also