String Data Type in Solidity

Solidity has a string data type, but it's not very commonly used. The problem is that Solidity has no native methods to work with strings like comparing or concatenating them. 

A better way to handle strings in Solidity is to store them in the byte data type. This is more efficient and requires less gas compared to the string data type. If you know the length of the string stored, you can select one of bytes1 to bytes32 to make the operation much cheaper.

Solidity allows us to use string literals in double quotes(") and single quotes (').

string test = "test"; // stored as string data type
bytes32 test = "test"; // stored as byte32 data type

String literals can be split into multiple parts:

 string test = "hello " "world" // same as "hello world"

Unicode String Literals

Regular strings can only contain ASCII characters in Solidity. Unicode literals are prefixed by the keyword unicode and allow any valid UTF-8 characters. string myUnicodeString = unicode"Hello World";



Click to jump to section