I'm learning solidity. I want to check if a memory string variable is a null, empty, or whitespaces. I understand I have to check it like this:
bytes(_content).length > 0
However, this does now cover empty whitespace. What would be the best way to check for empty whitespace? Or do you suggest that this check does not belong in s
if you convert "" to bytes you get 0x
if you convert " " to bytes you get 0x20
for each empty space, it adds another 20. for example 2 empty space
" " is 0x2020
empty string characters will always return "20" and as far as I know we could have slice operations with bytes array since solidity 6.0.0. For example
bytes test = '0xabcd'
test[2:5]; # 'abc'
Knowing this, if you have this
bytes whitespaces='0x20202020202020'
you could write a for loop, starting from 2 till the end,if the even index is 2 and odd index 0 that means you have only white spaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With