I want to add the extractedStr variable into the path?
bytes memory extractedStr;
string memory combine = '<path d="string(extractedStr)" fill="#FFFFFF"/>';
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityYour 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)'
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"/>