Skip to content

Text operations

Package: TEXT_PACKAGE. Registered by createEngine(); for a slimmer engine, register it explicitly (see choosing packages).

A piece of text in quotation marks, "hello", is a value the same way a number is. This page is what you can do with one: measure how long it is, ask whether it contains something, and reshape it, uppercase it, trim the stray spaces off it, turn a title into the hyphenated form a web address uses. It is the everyday string handling a note often needs alongside its sums, kept in the same place as the sums.

Everything here works on text. Give an operation a number or another kind of value and it reports an error rather than guessing, the same discipline the rest of the engine follows.

A plus joins text to text, end to end.

"hello" + " world" // hello world

The two sides must both be text to join. "a" + "b" is ab; when one side is a number the + is arithmetic, not a join, and the text is read as a number, so "5" + 5 is 10 and "hello" + 5 is 5 (the non-numeric text reads as 0). Keep both sides quoted for a text join.

length of counts the characters. words in, characters in and lines in count what they name, a word being a run of non-space characters, a line being a stretch between line breaks.

length of "hello" // 5
words in "the quick brown fox" // 4
characters in "hello" // 5

Counting is by character, not by byte, so an accent or an emoji counts as the one character it looks like rather than the two-or-more bytes it is stored as.

contains asks whether one piece of text appears inside another; starts with and ends with ask about the two ends. Each answers true or false, so they sit naturally inside a condition.

"hello" contains "ell" // true
"hello" starts with "he" // true
"report" ends with "port" // true

trim removes the leading and trailing spaces (the ones that creep in from a copy and paste); reverse turns the characters back to front; X repeated N times repeats the text.

trim " spaced out " // spaced out
reverse "hello" // olleh
"ha" repeated 3 times // hahaha

replace swaps every occurrence of one piece of text for another. It is written as a function, replace(text, find, replacement), rather than the sentence “replace A with B in C”, because “with” already means addition in this language (40 with 2 is 42), so the sentence form would be read as a sum.

replace("banana", "a", "@") // b@n@n@

The replacement is literal: find is matched exactly, character for character, with no pattern matching. (Regular expressions are a possible later addition.)

as upper, as lower, as title and as slug convert the case. A slug is the lowercase, hyphenated form a title takes in a web address, so "Hello, World!" becomes hello-world.

"hello world" as upper // HELLO WORLD
"HELLO" as lower // hello
"the lord of the rings" as title // The Lord Of The Rings
"Hello, World!" as slug // hello-world

Every measuring and reshaping form also has a call spelling, for when that reads more naturally in the middle of a longer line: length("hi"), upper("hi"), slug("A B C"), words("one two"), replace(...). They are the same operations under a different notation.

length("hello") // 5
upper("hi there") // HI THERE