Solidity: Playing With Strings

Getting started with Solidity

Rosario De Chiara
Better Programming

--

The word “WORLD” spelled in LED lights in a dark room.
Photo by Martim Braz on Unsplash.

Strings of characters represent the very heart of every programming language because, you know, computers often interact with humans. For this reason, handling strings is one of the first functionalities developed in any language. Furthermore, the first test traditionally prints the string “Hello, world!”

Solidity is not an exception. It has native support for strings, but still, their use is not complete like in high-level languages such as JavaScript, Python, or Java. For instance, out of the box, Solidity does not offer a native way of comparing or concatenating strings. However, the documentation offers a valid solution for both functions that rely on abi.encodePacked() (we’ll talk about it later) to compare two strings:

keccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2)) 

And to concatenate two strings:

abi.encodePacked(s1, s2)

To better understand the inner mechanics of these two simple functions, as usual, we will use a simple toy implementation:

As usual, you can comfortably play with it by using Remix. Once it is compiled, you will be able to invoke the two methods right from the interface:

Screenshot showing how to invoke the methods.
The interface to invoke the methods.

Calling the two methods will sign a transaction containing an invocation to the method passing the arguments. The output of the transaction is displayed in the console windows following the execution of the string concatenation (to fully display the results, remember to click on the small arrow on the right):

Decoded output.
Execution of the ‘concatenate’ method.

At the core of these two simple implementations is the function abi.encodePacked(), which performs a tight packing of the variables passed as arguments. In the following figure (taken from the documentation), you can see how the bits of each of the arguments are simply concatenated in the output:

Hand-written breakdown of how the arguments are packed.
An example of how the arguments passed to encodePacked are actually packed.

An interesting point in this encoding is that it is inherently ambiguous so there is no way of decoding it. In the following code, we simply test this ambiguity by comparing the encoding of two sets of different strings. With the in-place packing, the two invocations will return the exact sequence of bytes because both invocations will return the string "aab" without considering the different length of the input strings:

Comparing the results of the encoding by usingkeccak256() is a common approach in Solidity because of its gas efficiency.

Conclusion

Basic string manipulation is offered by Solidity not natively but through a pretty standardized use of some native and efficient use.

There are more sophisticated string manipulation libraries available. There is even a JSON parser library.

My personal advice is to keep this kind of manipulation as low as possible. Messages for the user, complex JSON manipulation, etc. should be kept away from the blockchain. Blockchain should be considered a specialized back end whose performances rely on the representation of each of the variables involved.

--

--

Distributed Ledger Surfer, Data Masseur, Distributed Systems Sculptor, and Scalability Evangelist