Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation in solidity?

how do i concatenate strings in solidity?

var str = 'asdf'
var b = str + 'sdf'

seems not to work..

I looked up the documentation (https://github.com/ethereum/wiki/wiki/Solidity-Tutorial#elementary-types-value-types) and there is not much mentioned about string concatenation. But it is stated that it works with the dot ('.') ?

"[...] a mapping key k is located at sha3(k . p) where . is concatenation."

Didn't work out for me too.. :/

like image 789
tObi Avatar asked Aug 22 '15 15:08

tObi


People also ask

How do you concatenate strings in Solidity?

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.

What is bytes32 in Solidity?

It is provided for storing information in binary format. Since the array is dynamic, its length can grow or shrink. To reflect this, Solidity provides a wide range — from bytes1 to bytes32 . The data type bytes1 represents one byte, while bytes32 represents 32B. 0 x 00 is the default value for byte.

How do you use require in Solidity?

'require' returns two boolean values that are either true or false, if the specified condition returns a true value it allows the code to flow and function accordingly. If the value returned is false, it throws an error and stops the code right there.

How do you use structs in Solidity?

To define a structure struct keyword is used, which creates a new data type. For accessing any element of the structure, 'dot operator' is used, which separates the struct variable and the element we wish to access. To define the variable of structure data type structure name is used.


2 Answers

An answer from the Ethereum Stack Exchange:

A library can be used, for example:

import "github.com/Arachnid/solidity-stringutils/strings.sol";

contract C {
  using strings for *;
  string public s;

  function foo(string s1, string s2) {
    s = s1.toSlice().concat(s2.toSlice());
  }
}

Use the above for a quick test that you can modify for your needs.


Since concatenating strings needs to be done manually for now, and doing so in a contract may consume unnecessary gas (new string has to be allocated and then each character written), it is worth considering what's the use case that needs string concatenation?

If the DApp can be written in a way so that the frontend concatenates the strings, and then passes it to the contract for processing, this could be a better design.

Or, if a contract wants to hash a single long string, note that all the built-in hashing functions in Solidity (sha256, ripemd160, sha3) take a variable number of arguments and will perform the concatenation before computing the hash.

like image 90
eth Avatar answered Sep 22 '22 07:09

eth


You can't concatenate strings. You also can not check equals (str0 == str1) yet. The string type was just recently added back to the language so it will probably take a while until all of this works. What you can do (which they recently added) is to use strings as keys for mappings.

The concatenation you're pointing to is how storage addresses are computed based on field types and such, but that's handled by the compiler.

like image 35
Andreas Olofsson Avatar answered Sep 24 '22 07:09

Andreas Olofsson