How to append the string to another in solidity| Solidity by example

This tutorial talks about how to do string concatenation in solidity. This explains how to append one string to another string in solidity.

For example, you have two strings.

string str = 'hello'
string b = "world"

And output is

hello world

It is very easy to append strings in another programming language such as java and javascript using the + or append function or method.

There is no inbuilt support for append function or + operator in solidity strings.

Please note that string manipulation is an expensive operation in gas operation, It is not recommended to use how blockchain or solidity works with string manipulation.

How to concatenate string in solidity with example

Solidity-stringutils library provides string manipulation functions. It provides strings.slice concat function.

The below function takes two strings Convert two solidity strings type object to strings.slice type using toSlice() function. Append the strings using the concat function.

Here is a code for string append example in solidity.

// SPDX-License-Identifier: GPL-3.0

pragma solidity ~0.4.14;
import "https://github.com/Arachnid/solidity-stringutils/blob/master/src/strings.sol";

contract MyContract {
  using strings for *;
  string public str;

  function append(string string1, string string2) {
    str = string1.toSlice().concat(string2.toSlice());
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.