I have a simple mapping:

 mapping(address => string) professorsExam;

How can I check if msg.sender is a key in this mapping, and if it is, get its value? If it isn't I should return something.

string memory professorsExamHash = professorsExam[msg.sender];  // The hash of the exam owned by msg.sender
if(professorsExamHash == 0x0){
    return "No exam hash asociated with this professor address";

This doesn't work,

TypeError: Operator == not compatible with types string memory and int_const 0

I have tried many other things. But == isn't compatible between anything so far.

1 Answer 1


You can check the length of the bytes of the value. You do this by converting the string to bytes and checking the length. If it is greater than 0, than it exists.

Your new code would be as follows:

if(bytes(professorsExamHash).length == 0){
    return "No exam hash asociated with this professor address";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.