Strings & Bytes
Strings
Strings are defined using the built-in string
type like this:
Concatenating Strings
Strings can be concatenated using the +
operator.
Extracting Substrings
Substrings can be extracted using the predefined function
String.sub
. The first character has index 0 and the interval of
indices for the substring has inclusive bounds.
⚠️ Notice that the offset and length of the slice are natural numbers.
Length of Strings
The length of a string can be found using a built-in function:
Bytes
Byte literals are defined using the prefix 0x
followed by hexadecimal digits like this:
Moreover, a string literal can be converted to its bytes representation:
Concatenating Bytes
Bytes can be concatenated using the Bytes.concat
function.
Extracting Bytes
Bytes can be extracted using the predefined function Bytes.sub
. The
first parameter takes the start index and the second parameter takes
the number of bytes. Pay special attention to how bytes
are
indexed.
Length of Bytes
The length of bytes
can be found using a built-in function Bytes.length
:
Bitwise operators
You can perform bitwise operation on bytes
as follows:
From bytes
to nat
and back
You can case bytes
to nat
using the built-in nat
function and vice-versa
using using the bytes
built-in function.
From bytes
to int
and back
You can cast bytes
to int
using the built-in int
function and
vice-versa using the bytes
built-in function.