Manual:DIL Manual/split()
Jump to navigation
Jump to search
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.