Skip to main content
Tolk provides a dedicated string type. At the TVM level, a string is a cell that stores data in snake format: a chain of cells where each cell holds a portion of the string and a reference to the next.

String literals

Every string literal produces a value of type string:
However, on the TVM, each literal is a cell:
A literal cell is created at compile-time, there is no expensive 500-gas cell creation at runtime.

Snake format

Since a single cell can hold at most 1023 bits (127 bytes), longer strings use the snake format: the start of the string is placed in the root cell, with its continuation in a referenced cell, recursively:
Short strings may also be stored in snake format — for example, after concatenation. The logical value "abcd" and the snaked "ab" -> ref "cd" are treated identically.

Traversing

To manually traverse contents of a string, call a beginParse() method that returns slice:

String methods

Standard library methods for strings require an explicit import:

string.calculateLength

Loops over all references and returns the total number of characters:
Both "abcd" and "a" -> ref "bc" -> ref "d" return 4.

string.equalTo

Compares two strings for equality, accounting for their snaked internals:
For instance, "--disable" equals "--dis" -> ref "able".

string.hash

Returns a hash of the string regardless of its internal representation. The hash of "abcd" and "a" -> ref "bcd" is identical.

Compile-time methods

Several methods operate on string literals at compile-time. Such strings are evaluated during compilation and embedded into the contract as constants.
These methods work only on constant strings: calling them on variables produces a compilation error.

StringBuilder

StringBuilder from @stdlib/strings concatenates several strings into one snake-encoded string:

StringBuilder.append

The method StringBuilder.append returns self and can be chained:
A practical example: an NFT collection with common content and per-item content:
The method StringBuilder.append always concatenates strings in the snake format. This does not affect the final representation, since both "abcd" and "ab" -> ref "cd" are the same logical string.

StringBuilder.appendInt

Stores an integer in decimal format. This method is gas-expensive. It is suitable for contract getters executed off-chain, but should be avoided in on-chain production environments:

TEP-compliant encoding

Per the token metadata standard, strings returned from get-methods must contain prefixes for off-chain explorers and APIs:
  • 0x00 + "<STRING>" — a regular snake-encoded string
  • 0x01 + "<STRING>" — an off-chain content URL
Prefixing does not create a new entity — the result remains a string, a concatenation of one character and the original content. Standard library contains an alias: type string_prefixed0x = string. Use this alias to indicate that a string is intentionally prefixed. Because it is an alias, the value remains a string (a TVM cell) at runtime and stays opaque to clients. Use specialized methods string.prefixWith00 and string.prefixWith01 for prefixing metadata strings:

Stack layout and serialization

A string is backed by the snake-encoded TVM CELL. Serialized as a single cell reference.