Skip to Content
Mastering Blockchain Programming with Solidity
book

Mastering Blockchain Programming with Solidity

by Jitendra Chittoda
August 2019
Intermediate to advanced
486 pages
13h 52m
English
Packt Publishing
Content preview from Mastering Blockchain Programming with Solidity

String equality comparison pattern

In Solidity, there is no native function to compare the strings. Hence, it is not possible to compare the equality of the two strings. There are ways to do this by checking byte-by-byte data, but it would be a costly operation in terms of gas consumption. Solidity is not ideal for this, so you should think carefully when using this.

To check for string equality, we can generate the keccak256 hash of both strings and compare the hashes with each other. The generated hash of the two same strings will always give the same hash value. If the strings are not equal, their hashes will also differ. 

The following code should be used to compare two strings for their equality:

function compare(string memory a, string ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Ethereum

Mastering Ethereum

Andreas M. Antonopoulos, Gavin Wood
Complete Cryptocurrency and Blockchain Course | Learn Solidity

Complete Cryptocurrency and Blockchain Course | Learn Solidity

Ravinder Deol, Codestars By Rob Percival, Thomas Wiesner

Publisher Resources

ISBN: 9781839218262Supplemental Content