Strings and interpolation
A string is a run of text. There are two ways to write one, and the difference is whether it interpolates.
The two forms
- Double quotes
"..."interpolate. Inside them,$nameis replaced by a variable's value and[cmd …]is replaced by a command's result. - Single quotes
'...'are literal. What you type is exactly the string — no$or[substitution happens.
do "say hello [name $actor]" # interpolates: "say hello Puff"
do 'say hello [name $actor]' # literal: the text [name $actor] is sent
Use single quotes for fixed text and for anything containing $ or [
that should stay literal; use double quotes when you want to splice values
in.
Interpolation
Inside a "..." string, two substitutions are recognized:
$nameor${name}— replaced by the variable's value, converted to text. In the bare$nameform the name runs only while characters are letters, digits, or_, so"$x-1"readsxthen appends the literal-1. The braced${name}form ends at the}, which is how you glue a name to following letters:"${count}coins".[cmd …]— the command is run and its result spliced in.
do "say [name $actor] is level [level $actor]"
Against a level-26 actor named Puff this sends "Puff is level 26." Any value converts to text for this purpose (see Values — conversion to text); that is the language's one automatic conversion.
To put a literal $ or [ into a double-quoted string, escape it: \$
and \[. Both quote forms also accept \n (line feed), \t (tab), and
\\ (backslash); a double-quoted string additionally takes \", and a
single-quoted one \'.
Building text — interpolate, don't concatenate
There is no concat command. To build a string from pieces, write a
double-quoted string and interpolate the pieces:
after command (say) {
let mood 'cheerful'
do "say I am feeling $mood today, [name $actor]."
}
This is the idiomatic way to assemble any dynamic message — for
echo, do, emote, and so on.
Strings are not iterable
A string is a single value. To walk its words, convert it with
words:
each [words "north east south west"] { <dir>
do "say I could go $dir."
}
See Iterables for why a bare string can't be iterated directly.
Comparing strings
eq compares strings by exact content;
streqi compares case-insensitively. Note that '2' (a
string) and 2 (an int) are never equal — [eq '2' 2] is false. To
parse a numeric string into an int, use int.