Strings and StringBuilders
Strings are immutable sequences of characters, which means that once a String
is created, it cannot be changed. Strings are useful to store text, and so they can be converted to Cell
type to be used as message bodies.
To be able to concatenate strings in a gas-efficient way, use a StringBuilder
.
To use String
literals directly, see: String literals.
beginString
Creates and returns an empty StringBuilder
.
Usage example:
beginComment
Creates and returns an empty StringBuilder
for building a comment string, which prefixes the resulting String
with four null bytes. This format is used for passing text comments as message bodies.
Usage example:
beginTailString
Creates and returns an empty StringBuilder
for building a tail string, which prefixes the resulting String
with a single null byte. This format is used in various standards like NFT or Jetton.
Usage example:
beginStringFromBuilder
Creates and returns a new StringBuilder
from existing StringBuilder
b
. Useful when you need to serialize an existing String
to a Cell
with some other data.
Usage example:
StringBuilder.append
Extension mutation function for the StringBuilder
.
Appends a String
s
to the StringBuilder
.
Usage example:
StringBuilder.concat
Extension function for the StringBuilder
.
Returns a new StringBuilder
after concatinating it with a String
s
. Can be chained, unlike StringBuilder.append()
.
Usage example:
StringBuilder.toString
Extension function for the StringBuilder
.
Returns a built String
from a StringBuilder
.
Usage example:
StringBuilder.toCell
Extension function for the StringBuilder
.
Returns an assembled Cell
from a StringBuilder
.
Usage example:
StringBuilder.toSlice
Extension function for the StringBuilder
.
Returns an assembled Cell
as a Slice
from a StringBuilder
. Alias to self.toCell().asSlice()
.
Usage example:
String.asSlice
Extension function for the String
.
Returns a Slice
from a String
by trying to pack all of its bits into a continuous list of Cells, each referencing the next one and opening them all for future parsing.
Note, that there’s no indication of how many bytes a particular character could take in the Slice
or how deep the list of references is going to be, so use this function only if you know what you’re doing.
Usage example:
String.asComment
Extension function for the String
.
Returns a Cell
from a String
by prefixing the latter with four null bytes. This format is used for passing text comments as message bodies.
Usage example:
String.fromBase64
Extension function for the String
.
Returns a Slice
out of the decoded Base64 String
. Alias to self.asSlice().fromBase64()
.
Note, that this function is limited and only takes the first bits of data from the given String
, without throwing an exception when the String
is larger (i.e. contains more than bits of data).
If the given String
contains characters not from the Base64 set, an exception with exit code 134 will be thrown: Invalid argument
.
Usage example:
Slice.asString
Extension function for the Slice
.
Returns a String
from a Slice
by trying to load all of its bits without looking for its references, if any.
Note, that this function doesn’t look at the references at all and is truncates its output to bits, so use it only if you know what you’re doing.
Usage example:
Slice.fromBase64
Extension function for the Slice
.
Returns a new Slice
out of the decoded Base64 Slice
.
Note, that this function is limited and only takes the first bits of data from the given Slice
, without throwing an exception if the Slice
has more data (i.e., when it has any references).
If the given Slice
contains characters not from the Base64 set, an exception with exit code 134 will be thrown: Invalid argument
.
Usage example:
Int.toString
Extension function for the Int
.
Returns a String
from an Int
value.
Usage example:
Int.toFloatString
Extension function for the Int
.
Returns a String
from an Int
value using a fixed-point representation of a fractional number, where self
is a significant part of the number and digits
is a number of digits in the fractional part.
More precisely, digits
is an exponentiation parameter of expression, which gives the represented fractional number when multiplied by the actual Int
value. Parameter digits
is required to be in the closed interval: digits
, otherwise an exception with exit code 134 will be thrown: Invalid argument
.
Usage example:
Int.toCoinsString
Extension function for the Int
.
Returns a String
from an Int
value using a fixed-point representation of a fractional number. Alias to self.toFloatString(9)
.
This is used to represent nanoToncoin Int
values using strings.
Usage example:
Address.toString
Extension function for the Address
.
Returns a String
from an Address
.
Usage example: