Repository navigation
Category String Interpolation
Home › All Categories › 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>3A 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".
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.
| 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.
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.
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.
| 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. |
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.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Android (experimental)
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Move Semantics
- Match
- Lambdas
- Block Strings
- String Interpolation
- Partial Types
- String Slices
- Array Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Time Zones
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Hardware
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- tiko test
- tiko doc
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing