ilusm.dev
Guide

Working with Strings

String literals, interpolation, and the full txt module - everything you need to work with text in ilusm.

String literals

Plain strings use double quotes. No interpolation happens inside them.

msg = "hello world"
path = "lib/config.json"
empty = ""

String interpolation

Prefix with $ to interpolate expressions inside {}:

name = "ibot"
port = 8080

prn $"hello {name}"           # "hello ibot"
prn $"server on port {port}"  # "server on port 8080"
prn $"1 + 2 = {1 + 2}"       # "1 + 2 = 3"
prn $"type: {typ(name)}"      # "type: str"

Use or inside interpolation for defaults:

user = nil
prn $"hello {user or "guest"}"   # "hello guest"

Concatenation

Use + to join strings. For building up longer strings, txt.bld() is more efficient.

full = "hello" + " " + "world"   # "hello world"

Length

prn len("hello")   # 5
prn len("")        # 0

len counts Unicode runes (characters), not bytes.

The txt module

Load with use txt. All string utility functions live here.

Split and join

use txt

parts = txt.spl("a,b,c", ",")      # ["a", "b", "c"]
back  = txt.jn(["a", "b", "c"], ",")  # "a,b,c"
lines = txt.spl("line1\nline2", "\n") # ["line1", "line2"]

Case

txt.upr("hello")   # "HELLO"
txt.lwr("HELLO")   # "hello"

Trim whitespace

txt.trm("  hello  ")   # "hello"

Search and check

txt.has("hello world", "world")   # tru
txt.pfx("hello", "he")            # tru  (starts with)
txt.sfx("hello", "lo")            # tru  (ends with)
txt.idx("hello", "ll")            # 2    (first index, -1 if none)
txt.cnt("banana", "a")            # 3    (count occurrences)

Replace

txt.rep("hello world", "world", "ilusm")   # "hello ilusm"

Substring

txt.sub("hello", 1, 3)   # "el"  (start inclusive, end exclusive)
txt.sub("hello", 2)      # "llo" (to end, second arg is optional)

Character access

txt.at("hello", 0)        # "h"   (single rune at index)
txt.lst("hi")             # ["h", "i"]  (list of chars, for loops)
txt.ord("A")              # 65   (codepoint of first char)
txt.chr(65)               # "A"  (codepoint to char)
txt.rev("hello")          # "olleh" (reverse string)

Repeat and pad

txt.rpt("ab", 3)          # "ababab"
txt.pad("7", 3, "0")     # "007"  (pad left to length)
txt.padr("hi", 5, ".")   # "hi..." (pad right to length)

String builder

For building strings piece by piece efficiently:

b = txt.bld()
b.put("hello")
b.put(" ")
b.put("world")
prn b.out()    # "hello world"
b.clr()        # reset the builder

Looping over characters

use txt

s = "hello"
c <- txt.lst(s):
    prn c    # h, e, l, l, o

Convert to / from string

str(42)       # "42"
str(tru)      # "tru"
str(nil)      # "nil"
str([1,2])    # "[1, 2]"

int("42")     # 42  (string to integer)

Common patterns

Parse CSV-like input

use txt

line = "alice,30,engineer"
parts = txt.spl(line, ",")
name = parts[0]   # "alice"
age  = int(parts[1])  # 30

Check if a string is empty

s = ""
if !s: prn "empty"
# or
if s == "": prn "empty"

Build a comma-separated string from a list

use txt
use trl

names = ["alice", "bob", "carol"]
result = txt.jn(names, ", ")   # "alice, bob, carol"