Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check empty string in solidity?

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

like image 953
Martijn Jan Jaap de Bruin Avatar asked Apr 12 '26 22:04

Martijn Jan Jaap de Bruin


1 Answers

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.

like image 183
Yilmaz Avatar answered Apr 15 '26 20:04

Yilmaz