🎓 A1Learner
🔍
0 of 50 lessons completed

Solidity Strings and Bytes

Strings are pieces of text that a contract can store and return. Under the text, Solidity stores the characters as bytes.

A string variable can start with text and change it

The message variable starts with one greeting. The changeMessage function replaces it with a new one.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract GreetingCard {
    string public message = "Hello";

    function changeMessage() public {
        message = "Welcome";
    }
}

Strings are compared through their fingerprints

The abi.encodePacked call turns text into raw bytes. The keccak256 call then makes a fingerprint of those bytes, and fingerprints can be compared with ==.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract NameCheck {
    string public savedName = "Solidity";

    function changeName() public {
        savedName = "Rust";
    }

    function isSolidity() public returns (bool) {
        return keccak256(abi.encodePacked(savedName)) == keccak256(abi.encodePacked("Solidity"));
    }
}

string.concat joins two pieces of text

The joined function returns one string built from two stored pieces. The word memory in its return line marks the text as a temporary copy.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract JoinedWords {
    string public firstPart = "Hello, ";
    string public secondPart = "world";

    function joined() public returns (string memory) {
        return string.concat(firstPart, secondPart);
    }
}

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract ProfileCard {
    string public name = "Ana";
    string public city = "Paris";

    function changeCity() public {
        city = "Rome";
    }

    function isNamedAna() public returns (bool) {
        return keccak256(abi.encodePacked(name)) == keccak256(abi.encodePacked("Ana"));
    }

    function label() public returns (string memory) {
        return string.concat(string.concat(name, " lives in "), city);
    }
}

Key points

  • String type: a variable written as string stores a piece of text.
  • Bytes: a byte is one small unit of data, and text is stored as a series of bytes.
  • Changing text: a function can replace a stored string with a new piece of text.
  • No direct compare: strings cannot be compared with == in Solidity.
  • Fingerprint compare: keccak256 turns packed text into a fingerprint, and two texts are equal when their fingerprints match.
  • Joining text: string.concat joins two pieces of text into one longer piece.

In this example, the card stores a name and a city as strings. The changeCity function replaces the city, isNamedAna compares fingerprints to check the name, and label joins the pieces into one sentence with string.concat.

Note

a function that returns a string writes returns (string memory), where memory marks a temporary copy used while the function runs.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Remix IDE

Exercise

How do you check whether two strings hold the same text in Solidity?