Strings
Basics
A string variable is written with $ (name$) or the .s tag (name.s). C style: string name. Strings hold UTF-16 text, are managed automatically, and start out empty.
OpenConsole()
first$ = "Ada"
last.s = "Lovelace"
full$ = first$ + " " + last
PrintN(full$ + " (" + Str(Len(full$)) + " characters)")
Output:
Ada Lovelace (12 characters)
name$ and name are two different variables. Once last.s is declared, last refers to it without the tag.
Joining
+ joins strings. A number on the other side is converted to text, as Str() or StrD() would:
OpenConsole()
n = 42
PrintN("n = " + n)
PrintN("half = " + (n / 4.0))
Output:
n = 42
half = 10.5
Comparing
=, <>, <, <=, > and >= compare strings character by character, by character code. Comparison is case-sensitive.
OpenConsole()
If "apple" < "banana" : PrintN("apple first") : EndIf
If "Zebra" < "apple" : PrintN("capitals sort first") : EndIf
If "a" <> "A" : PrintN("case matters") : EndIf
Output:
apple first
capitals sort first
case matters
Building strings in a loop
Appending to a variable is fast. The string keeps spare room and grows by doubling, so a loop that appends one piece at a time takes time proportional to the final length, not its square.
OpenConsole()
s$ = ""
For i = 1 To 10
s$ + Str(i % 10)
Next
PrintN(s$)
Output:
1234567890
s$ + x, s$ += x and s$ = s$ + x all append in place.
How strings work
A string variable holds a pointer to the first character. In front of the characters is a small header:
| Offset | Field |
|---|---|
| −24 | capacity: characters that fit without reallocating |
| −16 | length: the number of characters (kept up to date, so Len() doesn't count) |
| −8 | reference count |
The characters end with a 0, so @s$ is a normal null-terminated UTF-16 buffer that can be passed to the operating system.
Assigning a string to another variable, or passing it to a procedure, doesn't copy the characters. It shares them and increases the reference count. A copy is made only when one side changes a shared string. Each variable behaves as if it had its own copy.
String literals are stored once in the executable and never freed.
Characters
Chr(code) makes a one-character string. Asc(s$) gives the code of the first character. A character literal 'A' is the number 65.
OpenConsole()
PrintN(Chr('A' + 2) + " " + Str(Asc("a")))
Output:
C 97
String functions
See the String library: Len, Left, Right, Mid, Space, Str, StrD, StrF, Val, Chr, Asc.
Strings in structures, arrays, lists and maps
A string can be stored anywhere a number can: a structure field, an array element, a list or map element. The rules above still apply. The old string is released when a new one is stored, and every string is released when its container goes away.
Structure Person
name.s
age.i
EndStructure
Define p.Person
p\name = "Ada"
p\age = 36
p\name + " Lovelace"
OpenConsole()
PrintN(p\name + ", " + Str(p\age))
Output:
Ada Lovelace, 36
Not yet available 🔜
string8(.s8): 8-bit UTF-8 strings.- Fixed-length strings,
.s{N}. ~"…"literals with escapes.