PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 25, 2022

[FIXED] How do I check empty string in solidity?

 September 25, 2022     blockchain, ethereum, smartcontracts, solidity, string     No comments   

Issue

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


Solution

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.



Answered By - Yilmaz
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing