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 decimalx/X— hex lower/upper,o— octalf— fixed-point float,e/E— scientificg/G— general (shortest offore)
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 valuewrite_char(ch)— write a single characterwrite_chars(ch, count)— write a character N timeswrite_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