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?

2 Answers 2

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

    require(whereBytes.length >= whatBytes.length);

    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;
        if (flag) {
            found = true;
    require (found);


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

This repository include a function that find a word into a string and the number of repetitions.


Hope it helps

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.