3

I want to calculate string length in bytes. Here is the sample code I used.

function mine(string s  ) public view returns (bytes) {
        return bytes(s).length;
}

But throws this error-

TypeError: Return argument type uint256 is not implicitly convertible to expected type (type of first return variable) bytes memory.
         return bytes(s).length;
                ^-------------^
Divya Galla
  • 175
  • 1
  • 10

1 Answers1

9

If you want to return byte-length of the string, you will need to modify your return type like this:

function mine(string s) public view returns (uint256) {
  return bytes(s).length;
}

This method will only return byte-length of your string.

For UTF-string length, follow Alexey's answer in detail here: https://ethereum.stackexchange.com/a/13886/5018

Harsh Vakharia
  • 226
  • 1
  • 5
  • Hey, Harsh Vakharia can you answer a similar question- https://ethereum.stackexchange.com/questions/55086/calculate-integer-input-length – Divya Galla Jul 25 '18 at 09:20