0

Could you please help me in resolving the error "Parsing Error :Primary Expected Exception " below is very simple code

contract TestString {
    function stringConcat(string a, string b) public returns (string) {
        bytes memory strA = bytes(a);
        bytes memory strB = bytes(b);

        string memory finalLength = new string(a.lenght + b.lenght);

        bytes memory finalStr = bytes[finalLength];

        uint k = 0;

        for(uint i = 0; i < a.lenght; i++) {
            finalStr[k++] = strA[i];
        }

        for(i = 0; i < b.lenght; i++) {
            finalStr[k++] = strB[i];
        }

        returns string(finalStr);   
    }
}
  • That's not the error message. In the future, please copy/paste the actual error message. – smarx Jan 27 at 19:55
  • 2
    I haven't tried to compile your code, but you misspelled "length" multiple times. – smarx Jan 27 at 19:56
  • 1
    Also, strings don't have a length member in the first place. And you used i without declaring it, and you wrote "returns" instead of "return" on the last line... I'm sure you're new to programming, but start very small, start by modifying code that works, and build up. – smarx Jan 27 at 19:58
  • I am getting the below error browser/TestString.sol:17:8: ParserError: Expected primary expression. returns string(finalStr); ^-----^ – Punnu Sinha Jan 27 at 20:01
  • Below is the correct code pragma solidity ^0.5.0; contract TestString { function stringConcat(string a, string b)public returns (string){ bytes memory strA = bytes(a); bytes memory strB = bytes(b); string memory finalLength = new string(a.length+b.length); bytes memory finalStr = bytes[finalLength]; uint k=0; for(uint i=0;i<a.length;i++){ finalStr[k++] =strA[i];} for(i=0;i<b.length;i++) {finalStr[k++] =strB[i];} returns string(finalStr); } – Punnu Sinha Jan 27 at 20:02
1

Fixed code that works with Solidity 0.5.x. Note that in addition to fixing the compilation errors, I also renamed strA and strB to bytesA and bytesB, and I renamed finalStr to finalBytes. It seems better to have the variable names reflect their types.

pragma solidity ^0.5;

contract TestString {
    function stringConcat(string memory a, string memory b) public pure returns (string memory) {
       bytes memory bytesA = bytes(a);
       bytes memory bytesB = bytes(b);

       string memory finalLength = new string(bytesA.length + bytesB.length);

       bytes memory finalBytes = bytes(finalLength);

       uint256 k = 0;
       for(uint256 i = 0; i < bytesA.length; i++) {
           finalBytes[k++] = bytesA[i];
       }

       for(uint256 i = 0; i < bytesB.length; i++) {
           finalBytes[k++] = bytesB[i];
       }

       return string(finalBytes);
    }
}

EDIT

A little improvement below. finalLength was also misnamed because it was a string. But it wasn't necessary at all. See below:

pragma solidity ^0.5;

contract TestString {
    function stringConcat(string memory a, string memory b) public pure returns (string memory) {
        bytes memory bytesA = bytes(a);
        bytes memory bytesB = bytes(b);

        bytes memory finalBytes = new bytes(bytesA.length + bytesB.length);

        uint256 k = 0;

        for(uint256 i = 0; i < bytesA.length; i++) {
            finalBytes[k++] = bytesA[i];
        }

        for(uint256 i = 0; i < bytesB.length; i++) {
            finalBytes[k++] = bytesB[i];
        }

        return string(finalBytes);
    }
}
  • Thank you, actually i started with very basic setter and getter and was working fine.so thought of to learn and implement and good to know about more basics with this sample code and your help – Punnu Sinha Jan 27 at 20:15

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.