Difference between revisions of "Manual:DIL Manual/getwords()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = getwords() = | |
| − | + | '''Function''' (stringlist). Splits a string into its words. | |
| − | |||
| − | |||
| − | + | getwords(s : string) | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | Returns a stringlist with one entry per word of <code>s</code>, in order. Words are separated by spaces; any number of spaces counts as one separator, and no empty entries are made. <code>s</code> itself is not changed (unlike <code>getword()</code>). | |
| + | |||
| + | == Parameters == | ||
| + | {| class="wikitable" | ||
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | s || string || The text to split. | ||
| + | |} | ||
| + | |||
| + | == Returns == | ||
| + | Stringlist: the words. Empty when <code>s</code> is "" or only spaces. It fails if <code>s</code> 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: <code>getwords("a\nb")</code> is one entry. For lines, use <code>split(text, "\n")</code>, or <code>split(text, "<br/>")</code> for text kept as HTML. | ||
| + | * <code>length()</code> gives the number of words; <code>words.[0]</code> is the first. | ||
| + | |||
| + | == Example == | ||
| + | <pre> | ||
| + | dilbegin integer count_words(s : string); | ||
| + | var | ||
| + | words : stringlist; | ||
| + | code | ||
| + | { | ||
| + | words := getwords(s); | ||
| + | return (length(words)); | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | Counts the words in a string: " open the door " gives 3. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/getword()|getword()]] | ||
| + | * [[Manual:DIL Manual/split()|split()]] | ||
| + | * [[Manual:DIL Manual/length()|length()]] | ||
| + | * [[Manual:DIL Manual/in|in]] | ||
| + | * [[Manual:DIL Manual/stringlist|stringlist]] | ||
Latest revision as of 23:15, 14 November 2026
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, usesplit(text, "\n"), orsplit(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.