- Declare strings and choose
memoryorcalldatafor them. - Find a string’s byte length, compare two strings and join strings.
- Convert between
stringandbytes, and use escape sequences.
String
- Solidity data types are classified as value types and reference types. A
stringis a reference type: the variable refers to where the text is stored. - A string is a dynamically sized sequence of characters – letters, digits, spaces and symbols.
- Strings are stored as UTF-8 encoded bytes. Internally
stringis the same asbytes, but it does not allow index access or a.lengthmember. - Both double quotes
"Hello"and single quotes'Hello'can be used for string literals. Normal literals may contain only printable ASCII; for other characters use a Unicode literal:unicode"नमस्ते ₹".
String and Memory
- Because a string has no fixed size, you must tell the compiler where it is kept when it is used in a function:
memory,calldataorstorage. memoryis a temporary area (much like RAM). A function can use memory while it runs; after execution the memory is wiped.storagekeeps data between calls.- Return values of type string are always declared
string memory. - Parameters of external functions can use
string calldata, which is read-only and cheaper.
Common String Operations
| Task | How |
|---|---|
| Length (in bytes) | bytes(s).length – counts bytes, not characters; a non-ASCII character such as “₹” takes 3 bytes in UTF-8. |
| Compare two strings | keccak256(bytes(a)) == keccak256(bytes(b)) – the == operator does not work on strings. |
| Join (concatenate) | string.concat(a, " ", b) (Solidity 0.8.12+) |
| Read one byte | bytes(s)[0] returns a bytes1 |
| Bytes to string | string(b) where b is bytes memory |
| Empty check | bytes(s).length == 0 |
Bytes to String Conversion – bytes can be converted to a string with the string() conversion:
bytes memory bstr = new bytes(10);string memory message = string(bstr);
Escape Characters
| Sequence | Description |
|---|---|
\" | Double quote |
\' | Single quote |
\n | New line |
\\ | Backslash |
\t | Tab |
\r | Carriage return |
\xNN | Hex escape (one byte) |
\uNNNN | Unicode escape (inserted as UTF-8) |
\ + new line | Continues the literal on the next line |
Example : Working with Strings
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract StringDemo {
string public greeting = "Hello"; // stored permanently in storage
function setGreeting(string calldata text) external {
greeting = text; // calldata -> storage copy
}
function byteLength(string calldata text) external pure returns (uint) {
return bytes(text).length; // number of UTF-8 bytes
}
function isEqual(string memory a, string memory b) public pure returns (bool) {
return keccak256(bytes(a)) == keccak256(bytes(b));
}
function fullName(string memory first, string memory last) public pure returns (string memory) {
return string.concat(first, " ", last);
}
function firstLetter(string memory text) public pure returns (bytes1) {
require(bytes(text).length > 0, "Empty string");
return bytes(text)[0];
}
function rupee() public pure returns (string memory, uint) {
string memory symbol = unicode"₹";
return (symbol, bytes(symbol).length); // ("₹", 3)
}
}
Key Takeaways
- string is a dynamic UTF-8 reference type – always give it a data location in functions.
- Convert to
bytesfor length and indexing; compare strings with keccak256. - Use
string.concatto join andunicode"..."for non-ASCII text.
The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.
Question / Answer (15 questions)
bytes(s).length. This returns the number of bytes, which can be larger than the number of characters for non-ASCII text.keccak256(bytes(a)) == keccak256(bytes(b)). The == operator is not defined for strings.string.concat(a, b, ...), available from Solidity 0.8.12. Older code used string(abi.encodePacked(a, b)).bytes memory bstr = new bytes(10); string memory message = string(bstr);bytes(s)[0] returns the first byte as bytes1.unicode"नमस्ते ₹". Regular string literals may contain only printable ASCII characters (plus escape sequences).bytes(s).length == 0.MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- Solidity Team. (n.d.). Types (value types, reference types, mappings, operators, conversions). Solidity Documentation. https://docs.soliditylang.org/en/latest/types.html
- Solidity Team. (n.d.). Cheatsheet. Solidity Documentation. https://docs.soliditylang.org/en/latest/cheatsheet.html
- GeeksforGeeks. (n.d.). Solidity – Strings. https://www.geeksforgeeks.org/solidity-strings/
- Modi, R. (2018). Solidity Programming Essentials: A beginner’s guide to build smart contracts for Ethereum and blockchain. Packt Publishing. ISBN 978-1-78883-138-3.