Strings

Strings are immutable, double-quoted only (there's no single-quote string syntax), and can be indexed but not written to.

Literals & escapes

Falx recognises these escape sequences inside a double-quoted string - anything else after a backslash is a lexer error:

EscapeMeaning
\nNewline
\tTab
\rCarriage return
\fForm feed
\bBackspace
\"Literal double quote
\\Literal backslash

A literal, unescaped newline is also allowed directly inside a string (the string simply spans multiple lines):

let block = "line one
line two";

Indexing

Reading s[i] returns a one-character string; there's no separate "character" type. Writing to an index raises a runtime error, since strings are immutable:

let s = "Hello";
output(s[0]); // "H"
s[0] = "h"; // runtime error: 'string' is not indexable (for assignment)

length

output("Hello".length); // 5 - a property, not a method

Concatenation

+ on a string accepts another string, a number, or a boolean on the right-hand side, converting it to text - see Arithmetic.

output("Score: " + 42);      // "Score: 42"
output("Done: " + true);     // "Done: true"

Methods

toUpper() → string

Uppercases the string.

toLower() → string

Lowercases the string.

trim() → string

Strips leading and trailing whitespace.

contains(needle) → boolean

Whether needle occurs anywhere in the string.

startsWith(prefix) / endsWith(suffix) → boolean

Prefix/suffix checks.

replace(search, replacement) → string

Replaces every occurrence of search with replacement. Returns a new string - the original is untouched, since strings are immutable.

split(separator) → array

Splits on every occurrence of separator, returning an array of strings.

toInt() → number

Parses the string as an integer. Raises a runtime error if it isn't a valid integer literal.

toReal() → number

Parses the string as a float. Raises a runtime error if it isn't a valid numeric literal.

let s = "Hello, World!";
output(s.toUpper());              // HELLO, WORLD!
output(s.replace("World", "Falx")); // Hello, Falx!
output("a,b,c".split(","));       // ["a","b","c"]
output("42".toInt() + 1);          // 43

Truthiness recap

A string converts to true only if it is exactly "true" (case-insensitive) - see the full explanation on Truthiness before using a string as a while or &&/|| condition.