2

I want to add the extractedStr variable into the path?

bytes memory extractedStr;
string memory combine = '<path d="string(extractedStr)" fill="#FFFFFF"/>';

2 Answers 2

3

Your problem is basically a string concatenation problem.

Since Solidity v0.8.12, you concatenate strings easily using:

string.concat(s1, s2)

You can check solidity docs on strings and bytes for documentation reference.

In your case, this should help you concatenate your strings:

string public s1 = "string(";
string public s2 = ")";
bytes public yourBytes = '0x100';
string public combined;

function combine() public {
    combined = string.concat(s1, string(extractedStr), s2);
}

The combined string variable will now return:

'string(0x100)'
0

version 0.8.12 or higher

    function concatA() external pure returns (string memory) {
        bytes memory extractedStr = '0xFFFF00';
        return string.concat('<path d="', string(extractedStr) ,'" fill="#FFFFFF"/>');
    }

result 0:string: <path d="0xFFFF00" fill="#FFFFFF"/>

otherwise for earlier versions

    function concatB() external pure returns (string memory) {
        bytes memory extractedStr = '0xFFFF00';
        return string(abi.encodePacked('<path d="', string(extractedStr) ,'" fill="#FFFFFF"/>'));
    }

result 0: string: <path d="0xFFFF00" fill="#FFFFFF"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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