In Solidity, string concatenation can’t be done merely with the operator "+", unlike other high-level programming languages.
Let’s discuss how to perform the string concatenation in Solidity.
string.concat()
functionIf we are using Solidity v0.8.12 or a newer version, we can simply perform string concatenation with string.concat()
function.
/*SPDX-License-Identifier: Apache-2.0*/pragma solidity 0.8.12;contract Test {function concatenate(string memory s1, string memory s2) public pure returns (string memory) {return string.concat(s1,s2);}}
Line 3: We create a contract named Test
.
Line 4: We create a function named concatenate
.
Line 5: We call the string.concat()
function.
abi.encodePacked()
functionIn the Ethereum community, some ABI-encoded functions are available which can be used without calling another contract. We can concatenate strings with one of the ABI-encoded functions: abi.encodePacked()
. This is mostly used for Solidity versions earlier than v0.8.12.
The abi.encodePacked()
function returns the answer in byte type, therefore, we’ll cast it into the string type to get the desired output.
pragma solidity ^0.5.0;contract Test{function concatenate(string memory s1,string memory s2) public pure returns (string memory){return string(abi.encodePacked(s1,s2));}}
Line 3: We create a contract named Test
.
Line 4: We create a function named concatenate
.
Line 5: We call the abi.encodePacked()
function.
In this method, we create our own function which performs the concatenation operation.
First, we convert our two strings into byte arrays. These resulting byte arrays contains the ASCII representation of the characters in the string. Then we create a new string that stores the concatenated content. The memory value of this new string will be equal to the sum of the Byte length of the two strings. Next, we convert the new string into byte array. We iterate the byte values of the first string and save its content in the new string byte value. Similarly, we iterate the byte values of the second string and save its content in the new string byte value. As a result, the new string contains all the concatenated data.
The testConcatenate
function takes two string inputs (first and second), concatenates them using the concatenate
function from the Function
library, and returns the result as the output. This provides a convenient way to test the concatenate
function and see how it works.
pragma solidity ^0.5.0;library Function {function concatenate(string memory s1, string memory s2) pure internal returns (string memory) {bytes memory s1Bytes = bytes(s1);bytes memory s2Bytes = bytes(s2);string memory s3 = new string(s1Bytes.length + s2Bytes.length);bytes memory s3Bytes = bytes(s3);uint i;uint j;for(i=0;i<s1Bytes.length;i++) {s3Bytes[j++] = s1Bytes[i];}for(i=0;i<s2Bytes.length;i++) {s3Bytes[j++] = s2Bytes[i];}return string(s3Bytes);}}contract Test {using Function for string;function testConcatenate(string memory first,string memory second) public pure returns (string memory) {return first.concatenate(second);}}
Line 3: We create a library named Function
.
Line 4: We create a function named concatenate
.
Line 5: We convert s1
in bytes
memory.
Line 6: We convert s2
in bytes
memory.
Line 8: We create a new string to store the concatenated data whose memory value is equal to the sum of the byte length of s1
and s2
.
Line 9: We convert s3
in bytes
memory.
Line 11: We create an unsigned integer, i
, used for the iteration of s1
and s2
.
Line 12: We create an unsigned variable, j
, used for storing values in s3
.
Line 14: We iterate byte values of s1
.
Line 15: We copy the content of s1
in s3
.
Line 18: We iterate byte values of s2
.
Line 19: We copy the content of s2
in s3
.
Line 22: We cast the byte value of s3
in string and return it.
Line 25: We create a contract named Test
.
Line 27: We use Function
for string.
Line 29: We create a function named testConcatenate
.
Line 30: We return first.concatenate(second)
.