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 typestring:
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:"abcd" and the snaked "ab" -> ref "cd" are treated identically.
Traversing
To manually traverse contents of a string, call abeginParse() 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:
"abcd" and "a" -> ref "bc" -> ref "d" return 4.
string.equalTo
Compares two strings for equality, accounting for their snaked internals:
"--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.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:
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 string0x01+"<STRING>"— an off-chain content URL
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 TVMCELL. Serialized as a single cell reference.