59 - bytes & string
Variables of type bytes
and string
are special arrays
-
bytes
is similar tobyte[]
, but it is packed tightly in calldata and memory -
string
is equal tobytes
but does not allow length or index access -
Solidity does not have string manipulation functions, but there are third-party string libraries
-
Use
bytes
for arbitrary-length raw byte data andstring
for arbitrary-length string (UTF-8) data -
Use
bytes
overbyte[]
because it is cheaper, sincebyte[]
adds 31 padding bytes between the elements -
If you can limit the length to a certain number of bytes, always use one of the value types
bytes1
tobytes32
because they are much cheaper
- bytes -> bytes Data
- Arbitrary Data/Length
bytes1
,bytes2..bytes32
bytes
overbyte[]
-> Cheaperstring == bytes
- No length/Index Access
- Solidity -> No built-in String Manipulation Functions
- External Libraries
- Youtube Reference