Skip to content

Category String Interpolation

Paul Squires edited this page Nov 30, 2026 · 1 revision

Home › All Categories › String Interpolation

String Interpolation

#import "std/strings"

dim as ustring who = "Quinn"
dim as int32 n = 3
dim as float64 total = 41.5

print $"Hello, {who}!"                          ' Hello, Quinn!
print $"{n} items, {total:.2f} in all"          ' 3 items, 41.50 in all
print $"[{who,8}] [{who,-8}]"                   ' [   Quinn] [Quinn   ]
print $"{n * 2} / {ucase( who )} {{braces}}"    ' 6 / QUINN {braces}
print !$"{who}\t{n}"                            ' Quinn<TAB>3

A string literal written $"…" interpolates, as in C# and VB.NET: each {…} in it is a hole holding an expression, and the hole is replaced by the expression's value. The value is formatted by strings.Format, so everything Format accepts can go in a hole, and a hole can carry Format's spec after a colon.

$"…" with a hole needs #import "std/strings".

Holes

hole  ::=  '{' expression [',' alignment] [':' spec] '}'
Part Means
expression Any expression: a name, a + b, p->name, f( x, y ). It ends at the first ,, : or } outside parentheses and brackets.
,alignment C#'s alignment: ,8 right-aligns the value in 8 columns, ,-8 left-aligns it, whatever its type. An integer.
:spec Format's spec: {total:.2f}, {n:#06x}, {who:>10}, {who:?}. A spec may hold a hole of its own for a width or precision: {who:>{width}}.

{{ and }} are a literal brace. Each hole is evaluated once, left to right.

A hole cannot contain ", { or }. Put a string in a variable or a constant and name it in the hole. A hole that is only a literal, such as {3}, is an error (TKO1308): there is no reason to format a constant, and it is what a regular expression's quantifier looks like.

The Forms

Literal Holes Escapes
"…" no no
!"…" no yes
$"…" yes no
!$"…" yes yes
$""" … """ yes no
!$""" … """ yes yes

The last two are block strings that interpolate:

dim as ustring report = $"""
    Name:  {who}
    Total: {total,10:.2f}
    """

A hole in a block string stays on one line.

The Value

A literal with a hole is a call to strings.Format, and its value is a ustring. Assigning it to a string converts it as any ustring is converted.

A $"…" without a hole is an ordinary literal: $"plain" is "plain" and $"a {{b}}" is "a {b}". It can be used wherever a constant string can.

& joins an interpolated string to another string. Because it is a call, it does not join to a neighbouring literal by adjacency.

Checked When Compiled

A spec is checked against the value's type when the program is compiled, as for a Format call with a literal format string. The caret points at the character in the hole:

print $"value: {n:q}"
                  ^ error TKO0854: format string: 'q' is no presentation type for an integer

An error inside a hole's expression points into the hole, too.

Errors

Id When
TKO1300 A hole in a unit that has not imported std/strings.
TKO1301 A } with no {. Write }} for a brace.
TKO1302 A " or a { inside a hole.
TKO1303 An empty hole: {}, {:x}.
TKO1304 An interpolated string in #if.
TKO1305 An interpolated string as a const's value.
TKO1306 An interpolated string passed as a format string: it is already formatted.
TKO1307 A { whose hole does not close before the literal ends.
TKO1308 A hole that is only a literal.
TKO1309 An alignment that is not an integer, or an alignment beside a spec that sets its own alignment, 0 padding or width.

Differences from fbc

In fbc, $"…" is a literal with no escape processing. Under #lang "fb" every literal already has none, so $"…" meant exactly what "…" means, and tiko gives it the meaning C# does. A $"…" that contains no brace is unchanged. One that contains a brace now interpolates, or is an error. To keep it as text, write "…" (the same literal it was, in both compilers) or double the braces.

FreeBASIC code writes such literals for WMI monikers ("winmgmts:{impersonationLevel=impersonate}") and regular expressions ("\d{3}"). A hole holding only a literal, which is what a regex quantifier looks like, is TKO1308, so none of them changes meaning silently. AfxNova's own headers write them as "…".

!$"…" is new: fbc rejects it.

See Compatibility.

Clone this wiki locally