TEAL byte slices are similar to strings and can be manipulated in the same way.
The length of a byte slice can be obtained using the Len
expression. For example:
Len(Bytes("")) # will produce 0
Len(Bytes("algorand")) # will produce 8
Byte slices can be combined using the Concat
expression. This expression takes at least two arguments and produces a new byte slice consisting of each argument, one after another. For example:
Concat(Bytes("a"), Bytes("b"), Bytes("c")) # will produce "abc"
Byte slices can be extracted from other byte slices using the Substring
and Extract
expressions. These expressions are extremely similar, except one specifies a substring by start and end indexes, while the other uses a start index and length. Use whichever makes sense for your application.
The Substring
expression can extract part of a byte slice given start and end indices. For example:
Substring(Bytes("algorand"), Int(2), Int(8)) # will produce "gorand"
Note
Extract
is only available in TEAL version 5 or higher.
The Extract
expression can extract part of a byte slice given the start index and length. For example:
Extract(Bytes("algorand"), Int(2), Int(6)) # will produce "gorand"
The individual bits and bytes in a byte string can be extracted and changed. See bit_and_byte_manipulation
for more information.