1

I am just adding 2 strings into one string but getting below error:

transact to TestStrings.testCOntract errored: Error encoding arguments: SyntaxError: Unexpected token e in JSON at position 2

Below is my contract code:

pragma solidity ^0.5.1;

library Strings {

    function concat(string memory _base, string memory _value) internal returns (string memory) {

        bytes memory _baseBytes = bytes(_base);
        bytes memory _valueBytes = bytes(_value);

        string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
        bytes memory _newValue = bytes(_tmpValue);

        uint i;
        uint j;

        for(i = 0;i<_baseBytes.length;i++) {
            _newValue[j++] = _baseBytes[i];
        }

        for(i = 0;i<_valueBytes.length;i++) {
            _newValue[j++] = _baseBytes[i];
        }
        return string(_newValue);
    }
}

contract TestStrings {

    using Strings for string;

    function testCOntract(string memory _base) public returns(string memory) {
        return _base.concat("_suffix");
    }
}

I have tried THIS accepted answer and it's working fine. But I am not sure what I am missing with my code.

5
  • how are you calling the testCOntract? with web3js? i get sometimes that error when i dont give a valid string as parameter
    – Majd TL
    Commented Jun 11, 2019 at 9:17
  • I am using JavaScript VM Commented Jun 11, 2019 at 9:18
  • can you post your code somewhere, i think the error is not in solidity code
    – Majd TL
    Commented Jun 11, 2019 at 9:57
  • Sorry I am not getting you because I have already posted my code in my question. Commented Jun 11, 2019 at 9:58
  • your javascript code, not solidity :), or are you doing this with remix?
    – Majd TL
    Commented Jun 11, 2019 at 11:08

1 Answer 1

1

You Error is in the second for, it should be _valueBytes[i], and use pure in your function

pragma solidity ^0.5.1;

library Strings {

    function concat(string memory _base, string memory _value) internal pure returns (string memory) {

        bytes memory _baseBytes = bytes(_base);
        bytes memory _valueBytes = bytes(_value);

        string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
        bytes memory _newValue = bytes(_tmpValue);

        uint i;
        uint j;

        for(i = 0;i<_baseBytes.length;i++) {
            _newValue[j++] = _baseBytes[i];
        }

        for(i = 0;i<_valueBytes.length;i++) {
            _newValue[j++] = _valueBytes[i];
        }
        return string(_newValue);
    }
}

contract TestStrings {

    using Strings for string;

    function testCOntract(string memory _base) public pure returns(string memory) {
        return _base.concat("_suffix");
    }
}

enter image description here

2
  • Great and that was a silly mistake.. :) Commented Jun 11, 2019 at 11:27
  • welcome :P happen to everyone
    – Majd TL
    Commented Jun 11, 2019 at 11:27

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.