Published in: Ethereum Smart Contract Development Part 5: String Concatenation – Solidity
Last time, we implemented a simple smart contract. The contract returns exactly what the user enters as a string. A question is posed at the end of the article: if we define the Hello string in the contract, how do we concatenate the string with the name variable?
Concatenating strings in a smart contract is no simple matter. This article introduces string concatenation in smart contracts.
try
We first try to modify the code with the + and. Concatenators that are common in most languages:
Pragma keyword: version declaration.
// is used to instruct the compiler to compile code to a specific version so as not to cause compatibility problems
// Compilers before 0.4.0 and after 0.5.0 are not supported here (condition: ^)
pragma solidity ^0.4. 0;
//contract keyword: contract statement
// This is similar to class in Java and PHP
// declare a contract named Hello
contract Hello {
string str="Hello ";
//public: function access attributes (more on this later)
//returns (string): Defines the return value type as string
function say(string name) public returns (string) {
returnstr + name; }}Copy the code
After running the node deploy.js deployment script, an exception is thrown:
TypeError: Operator + not compatible with types string storage ref and string memory
return str + name;
After we change it to. And try to deploy, we also throw an exception:
TypeError: Member “name” not found or not visible after argument-dependent lookup in string storage ref return str . name;
Note In smart contracts, ***+*** * and. Are not connectives. Looking at the official Solidity documentation, we see that the Solidity language does not provide a syntax for string concatenators:
The following section is for paid users only benefits, please click the link to jump to my column to pay for purchase. By reading the following sections, you will learn:
- Third-party string tool contract: extended string processing (find, split, compare, concatenate, etc.)
- The introduction of third party contracts in smart contracts
- String concatenation implementation
My column: Smart contract Smart contract development QQ group: 753778670