String Concatenation in Solidity

Solidity does not offer a convenient built-in method to concatenate or compare the strings like other high-level programming languages such as java, Python, C or javascript, etc. the string type has just recently been introduced to the solidity language and it may take a while to improve further. However, the string concatenation is still possible with some other libraries and contracts available in solidity.

Method 1: abi.encodePacked()

Let’s visit remix.ethereum.org. we will use pragma solidity 0.8.0 version. Now we will introduce a contract called concat for the purpose of string concatenation. Let’s write a function concatenate” inside the concat” contract. A low-level function called abi.encodePacked can be used as the first method to concatenate the string.

pragma solidity ^0.8.0;
contract concat{
    function concatenate(string memory a,string memory b) public pure returns (string memory){
        return string(abi.encodePacked(a,' ',b));
    } 
}

We have used two variables “a” and “b” as memory and used them inside the abi.encodePacked to concatenate the strings. We used space(' ') in between the variables just to keep them separate while attaching.

As we can see from the left side of the above image, we deployed the contract in ethereum and used the variables "hello" and "finxters" to concatenate. The result comes as the concatenated string "hello finxters".

Method 2: bytes.concat()

Since it’s not possible to concatenate the strings with β€œ+” or β€œappend” in solidity , we will use the memory bytes to concatenate the strings in method 2. A special method called bytes.concat  allows us to easily concatenate the strings in solidity.

pragma solidity ^0.8.0;
contract concat {
    function concatenate(string memory a,string memory b) public pure returns (string memory){
        return string(bytes.concat(bytes(a), " ", bytes(b)));
    } 
}

We used the memory bytes of two variables a and b inside the bytes.concat method to concatenate the strings.

We tried to concatenate the string “know your” and "worth". it gives us the result "know your worth".

Method 3: Iteration

In this method, we will first convert the values of strings in bytes and then we will create a new variable to store the concatenated data.

We will add the length of two original string bytes and store it as a string memory value and then again convert it into a new bytes value like the two original strings given.

Then to concatenate, we will iterate over the two bytes values, converting the string values in bytes allows us to iterate over the two bytes values wherein it is not possible in string type.

pragma solidity ^0.8.0;
library Strings {
    function concat(string memory _x, string memory _y) pure internal returns (string memory) {
        bytes memory _xBytes = bytes(_x);
        bytes memory _yBytes = bytes(_y);
        
        string memory _tmpValue = new string(_xBytes.length + _yBytes.length);
        bytes memory _newValue = bytes(_tmpValue);
        
        uint i;
        uint j;
        
        for(i=0;i<_xBytes.length;i++) {
            _newValue[j++] = _xBytes[i];
        }
        
        for(i=0;i<_yBytes.length;i++) {
            _newValue[j++] = _yBytes[i];
        }
        
        return string(_newValue);
    }
}
contract TestStrings {
    
    using Strings for string;
    
    function testConcat(string memory _firstPart,string memory _lastPart) public pure returns (string memory) {
        return _firstPart.concat(_lastPart);
    }
}

As we can see from the code snippet, we have created two unsigned integers “i” and “j” while β€œj” is the iterator over the _newValue and “i” is the iterator over each of the individual arrays of strings we already have.

In the last part, we created a smart contract named testConcat and tried to concatenate two strings named _firstPart and _lastPart. After deploying the contract we tried to add two strings "finx" and "ter". It successfully returned the concatenated string "finxter" as a result.

That’s all about the string concatenation for today. If you want to know more, The following links may be helpful.

Learn Solidity Course

Solidity is the programming language of the future.

It gives you the rare and sought-after superpower to program against the “Internet Computer”, i.e., against decentralized Blockchains such as Ethereum, Binance Smart Chain, Ethereum Classic, Tron, and Avalanche – to mention just a few Blockchain infrastructures that support Solidity.

In particular, Solidity allows you to create smart contracts, i.e., pieces of code that automatically execute on specific conditions in a completely decentralized environment. For example, smart contracts empower you to create your own decentralized autonomous organizations (DAOs) that run on Blockchains without being subject to centralized control.

NFTs, DeFi, DAOs, and Blockchain-based games are all based on smart contracts.

This course is a simple, low-friction introduction to creating your first smart contract using the Remix IDE on the Ethereum testnet – without fluff, significant upfront costs to purchase ETH, or unnecessary complexity.

Programmer Humor – Blockchain

“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source xkcd