0

How is it possible to look for if a certain word is part of a string in solidity? let's say someone upload a string to a smart contract, this string must contain the word "solidity" to be accepted by a function. Is it possible to create a modifier or require() check that ensures this?

1
modifier contains (string memory what, string memory where) {
    bytes memory whatBytes = bytes (what);
    bytes memory whereBytes = bytes (where);

    bool found = false;
    for (uint i = 0; i < whereBytes.length - whatBytes.length; i++) {
        bool flag = true;
        for (uint j = 0; j < whatBytes.length; j++)
            if (whereBytes [i + j] != whatBytes [j]) {
                flag = false;
                break;
            }
        if (flag) {
            found = true;
            break;
        }
    }
    require (found);

    _;
}

function foo (string memory str) public contains ("solidity", str) {
    ...
}
  • You're a legend, your inputs are always appreciated. – NowsyMe Apr 5 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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