88 votes

How to concatenate strings in solidity?

Update: As of Solidity v0.8.12 the string.concat(a,b) function is the preferred method, as mentioned here. Optimized for low gas-consumption The ethereum community has implemented some ABI encoding ...
Lucas Aschenbach's user avatar
67 votes
Accepted

Use string type or bytes32?

Why string instead of bytes32? Use string for arbitrary-length string (UTF-8) data that's longer than 32 bytes. Frontends can decode a long string easier using methods like web3.toAscii or UTF-8 (...
eth's user avatar
  • 85.7k
66 votes

operator == not compatible with type string storage ref and literal_string

If you don't want to load the StringUtils contract, I found it's also possible to simply: keccak256(portcheck) == keccak256("signed"). Can anyone confirm that this would also cost less gas than a ...
Greg Mikeska's user avatar
62 votes
Accepted

How to convert a string to bytes32?

This has worked for me so far. Not sure if it's the best way. function stringToBytes32(string memory source) public pure returns (bytes32 result) { bytes memory tempEmptyStringTest = bytes(source)...
Grzegorz Kapkowski's user avatar
43 votes
Accepted

Convert address to string

Use abi.encodePacked(x) where x is the address. (Thanks @k06a)
eth's user avatar
  • 85.7k
42 votes
Accepted

How can you check if a `string` is empty in Solidity

You can convert the string into a type bytes and then check its length. For example this will test if emptyStringTest is indeed an empty string: bytes memory tempEmptyStringTest = bytes(...
41 votes

How to convert a bytes32 to string

As of feb 2021 you can do bytes32 foo = "hello"; string memory bar = string(abi.encodePacked(foo));
e18r's user avatar
  • 728
40 votes
Accepted

How to concatenate strings in solidity?

As of Feb 2022, in Solidity v0.8.12 you can now concatenate strings in a simpler fashion! string.concat(s1, s2) Taken directly from the solidity docs on strings and bytes: Solidity does not have ...
Freddie von Stange's user avatar
39 votes
Accepted

Why do Solidity examples use bytes32 type instead of string?

2 main reasons: Contracts currently cannot read a string that's returned by another contract. The EVM has a word-size of 32 bytes, so it is "optimized" for dealing with data in chunks of 32 bytes. (...
eth's user avatar
  • 85.7k
31 votes

How to concatenate strings in solidity?

You can do that in Solidity by copying each byte of the input strings to the output one. You can find an implementation of this in oraclizeAPI for example (function strConcat). The code is pretty ...
Thomas Bertani's user avatar
31 votes

How to convert a bytes32 to string

Here's one: function bytes32ToString(bytes32 x) constant returns (string) { bytes memory bytesString = new bytes(32); uint charCount = 0; for (uint j = 0; j < 32; j++) { byte ...
eth's user avatar
  • 85.7k
31 votes
Accepted

What is the zero value for a string?

One way is to check for the length of a String: if (bytes(m1[msg.sender]).length != 0 && bytes(m2[msg.sender].someString).length != 0) // do your thing See the answer I posted here
dbryson's user avatar
  • 6,403
30 votes

Is it possible to check string variable's length inside the contract?

Code for calculating string length in characters contract utf8StringLength { //---------------------BEGIN Code to copy-paste-------------- function utfStringLength(string str) constant ...
Alexey Barsuk's user avatar
28 votes

Convert address to string

I was not able to read the ABI-encoded string with web3.js. Therefore, I added some conversion to the ASCII characters: function toAsciiString(address x) internal pure returns (string memory) { ...
tkeber's user avatar
  • 381
26 votes

Use string type or bytes32?

Like the other post said, you only want to use strings for dynamically allocated data, otherwise Byte32 is going to perform better. Bytes32 is also going to be better in gas. If you want to play ...
Matthew Campbell's user avatar
26 votes
Accepted

Is it possible to check string variable's length inside the contract?

Just check if bytes(str).length is too big. Mind: This does not show the number of characters! See the answer below if you need to know an exact length of a utf-8 encoded string. This will cost ...
Tjaden Hess's user avatar
  • 37.1k
25 votes
Accepted

How to convert a bytes32 to string

Based on the latest compiler version 0.4.24, I use the following. function convertingToString()public returns(string){ bytes32 memory hw = "Hello World"; string memory converted = string(hw);...
R.D's user avatar
  • 505
24 votes

Indexed Event with string not getting logged

The combination of string and indexed does not work. To understand why, see how event arguments are stored in the blockchain. All transactions that are executed generate a transaction receipt, which ...
Mark van Cuijk's user avatar
23 votes

How to convert a string to bytes32?

string is not equal to bytes32 but it is equal to bytes, because its length is dynamic. so you could use a casting bytes B=bytes(S); //S string E.g contract string_test { function ...
Badr Bellaj's user avatar
  • 18.8k
22 votes

How to convert a string to bytes32?

ethers.js has a built in utility function for this: https://docs.ethers.io/ethers.js/html/api-utils.html#bytes32-strings const ethers = require('ethers') const utils = ethers.utils const inBytes = ...
bornSwift's user avatar
  • 329
21 votes
Accepted

How many letters can bytes32 keep?

Each bytes32 can store up to 32 letters (ASCII): each character is a byte.
eth's user avatar
  • 85.7k
18 votes

How to concatenate strings in solidity?

string s = string(abi.encodePacked("a", " ", "concatenated", " ", "string")); ethfiddle
gdether's user avatar
  • 181
18 votes

Convert address to string

This is method to convert address to hex string: function toString(address account) public pure returns(string memory) { return toString(abi.encodePacked(account)); } function toString(uint256 ...
k06a's user avatar
  • 3,016
17 votes

Conversion of uint to string

Another option is to use a solution from Oraclize https://github.com/oraclize/ethereum-api/blob/master/oraclizeAPI_0.5.sol, it suits best for me: 0.5 Compiler Version: function uint2str(uint _i) ...
Dmitriy Vinokurov's user avatar
16 votes
Accepted

operator == not compatible with type string storage ref and literal_string

As in Java, the == operator does not compare the literals of two strings. You should use the StringUtils contract instead. if (StringUtils.equal(portcheck,"signed")) {...}
Roland Kofler's user avatar
15 votes
Accepted

Convert with JS a Solidity bytes32 to a Solidity string

Try using the web3.js function web3.toAscii (doc), which might change name to web3.toUtf8 or something similar in the future.
MrChico's user avatar
  • 1,786
15 votes

Convert address to string

You can cast address to uint160, then use OpenZeppelin Strings library. Strings.toHexString(uint160(address), 20) Reference: Casting address to uint OpenZeppelin Strings for bytes
taijusanagi's user avatar
15 votes

String to Hex in Ethers.js

Good news! Ethers can natively read strings like "0x123beef" as a datatype it calls a HexString (source) Once you have a HexString, the docs for the available functions/methods are here. ...
Linum Labs's user avatar
  • 1,448
13 votes

How to concatenate strings in solidity?

To be honest, I don't think you should do string manipulation in the contract (which is on the blockchain). A well-developed blockchain app should be kicked off from a user account, not a contract. ...
reverendmed's user avatar
13 votes
Accepted

Is it impossible to use an array of strings as the argument to solidity function?

No, I don't believe so. As I understand it, Solidity doesn't have a built-in way yet to deserialize string arrays. It can cheat when the contract is passing an array it created itself to itself, which ...
ryepdx's user avatar
  • 1,342

Only top scored, non community-wiki answers of a minimum length are eligible