5.1.23. String Builder and Formatting

This tutorial covers format specifiers in string interpolation and programmatic string construction with build_string.

5.1.23.1. Format specifiers

Inside {expr} interpolation, add :flags width.precision type:

let x = 42
print("{x:08x}\n")      // 0000002a
print("{3.14:.2f}\n")   // 3.14

Flags:

  • 0 — zero-pad

  • - — left-align

  • + — force sign

Type characters:

  • d/i — signed decimal, u — unsigned decimal

  • x/X — hex lower/upper, o — octal

  • f — fixed-point float, e/E — scientific

  • g/G — general (shortest of f or e)

5.1.23.2. Escaping braces

Use \{ and \} for literal curly braces:

print("\{value\}\n")      // prints: {value}
print("\{{x}\}\n")        // prints: {42}

5.1.23.3. build_string

build_string constructs a string efficiently using a writer:

require strings

var csv = build_string() $(var writer : StringBuilderWriter) {
    writer |> write("name,score\n")
    writer |> write("Alice,95\n")
}

Writer functions:

  • write(value) — write any printable value

  • write_char(ch) — write a single character

  • write_chars(ch, count) — write a character N times

  • write_escape_string(str) — write with escape sequences visible

See also

String builder in the language reference.

Full source: tutorials/language/23_string_format.das

Next tutorial: Pattern Matching