Difference between revisions of "Manual:DIL Manual/split()"
Jump to navigation
Jump to search
(checked against the compiler and the interpreter; example compiles) |
|||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | = | + | = split() = |
| − | + | '''Function''' (stringlist). Cuts a string into pieces at a separator. | |
| − | |||
| − | + | split(s : string, sep : string) | |
| + | |||
| + | Returns the parts of <code>s</code> between the occurrences of <code>sep</code>, in order. The separator can be several characters long (<code>"<br/>"</code>, <code>"\n"</code>, <code>", "</code>) and is matched with case. If <code>sep</code> doesn't occur, the result has one entry, the whole string. | ||
| + | |||
| + | == Parameters == | ||
| + | {| class="wikitable" | ||
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | s || string || The text to cut. | ||
| + | |- | ||
| + | | sep || string || The separator. Must not be empty. | ||
| + | |} | ||
| + | |||
| + | == Returns == | ||
| + | Stringlist: the pieces. It fails if <code>s</code> or <code>sep</code> fails or is a string variable that has never had a value; assigning a failed list leaves the variable as it was. | ||
| + | |||
| + | == Notes == | ||
| + | * Each piece is tidied: spaces at its start and end are removed and runs of spaces inside it become one. So <code>split(text, sep)</code> can't be used to put text back together exactly. | ||
| + | * Empty pieces are kept: <code>split("a,,b", ",")</code> has three entries, the middle one "", and <code>split("a,", ",")</code> ends with "". <code>split("", ",")</code> has one entry, "". | ||
| + | * Don't pass an empty separator: the server's search reads past its end (see the source, <code>str_str</code> in textutil.cpp), and what happens is not defined. | ||
| + | * To split into words, <code>getwords()</code> is simpler: it drops empty entries. | ||
| + | * A file read with <code>loadstr()</code> is usually split into lines with <code>split(buf, "\n")</code> (clans.zon) or, for text kept as HTML, <code>split(buf, "<br/>")</code> (boards.zon). | ||
| + | |||
| + | == Example == | ||
| + | <pre> | ||
| + | dilbegin show_list(pc : unitptr, csv : string); | ||
| + | var | ||
| + | sl : stringlist; | ||
| + | i : integer; | ||
| + | n : integer; | ||
| + | code | ||
| + | { | ||
| + | sl := split(csv, ","); | ||
| + | n := length(sl); | ||
| + | i := 0; | ||
| + | while (i < n) | ||
| + | { | ||
| + | sendtext(itoa(i + 1) + ". " + sl.[i] + "<br/>", pc); | ||
| + | i := i + 1; | ||
| + | } | ||
| + | return; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | Shows "sword, shield, helmet" as a numbered list of three lines. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/getwords()|getwords()]] | ||
| + | * [[Manual:DIL Manual/getword()|getword()]] | ||
| + | * [[Manual:DIL Manual/loadstr()|loadstr()]] | ||
| + | * [[Manual:DIL Manual/length()|length()]] | ||
| + | * [[Manual:DIL Manual/replace()|replace()]] | ||
| + | * [[Manual:DIL Manual/stringlist|stringlist]] | ||
Latest revision as of 23:15, 14 November 2026
split()
Function (stringlist). Cuts a string into pieces at a separator.
split(s : string, sep : string)
Returns the parts of s between the occurrences of sep, in order. The separator can be several characters long ("<br/>", "\n", ", ") and is matched with case. If sep doesn't occur, the result has one entry, the whole string.
Parameters
| Name | Type | Meaning |
|---|---|---|
| s | string | The text to cut. |
| sep | string | The separator. Must not be empty. |
Returns
Stringlist: the pieces. It fails if s or sep fails or is a string variable that has never had a value; assigning a failed list leaves the variable as it was.
Notes
- Each piece is tidied: spaces at its start and end are removed and runs of spaces inside it become one. So
split(text, sep)can't be used to put text back together exactly. - Empty pieces are kept:
split("a,,b", ",")has three entries, the middle one "", andsplit("a,", ",")ends with "".split("", ",")has one entry, "". - Don't pass an empty separator: the server's search reads past its end (see the source,
str_strin textutil.cpp), and what happens is not defined. - To split into words,
getwords()is simpler: it drops empty entries. - A file read with
loadstr()is usually split into lines withsplit(buf, "\n")(clans.zon) or, for text kept as HTML,split(buf, "<br/>")(boards.zon).
Example
dilbegin show_list(pc : unitptr, csv : string);
var
sl : stringlist;
i : integer;
n : integer;
code
{
sl := split(csv, ",");
n := length(sl);
i := 0;
while (i < n)
{
sendtext(itoa(i + 1) + ". " + sl.[i] + "<br/>", pc);
i := i + 1;
}
return;
}
dilend
Shows "sword, shield, helmet" as a numbered list of three lines.