只是想知道是否有一种方法可以将长字符串拆分为多行?我找不到任何类型的行继续字符,如果您尝试使用这样的两行,则会抛出编译错误。连接字符串似乎也很复杂
string memory s = "This is a very long line of text which I would like to split over
several lines";
Run Code Online (Sandbox Code Playgroud)
连接字符串似乎也很复杂。我只需要将很长的字符串放在很长的线上吗?
Pet*_*jda 10
您可以将该值拆分为多个单独的字符串文字,每个字符串文字占一行。
pragma solidity ^0.8;
contract MyContract {
string s = "This is a very "
"long line of text "
"which I would like to split "
"over several lines";
}
Run Code Online (Sandbox Code Playgroud)
文档:https ://docs.soliditylang.org/en/v0.8.6/types.html#string-literals-and-types
如果要连接多个字符串,可以使用abi.encodePacked()返回bytes数组的方法,然后将其转换bytes回string.
pragma solidity ^0.8;
contract MyContract {
string s1 = "Lorem";
string s2 = "ipsum";
function foo() external view returns (string memory) {
return string(abi.encodePacked(s1, " ", s2));
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:从 v0.8.12 开始,您还可以使用string.concat()(以前版本中不可用)。
pragma solidity ^0.8.12;
contract MyContract {
string s1 = "Lorem";
string s2 = "ipsum";
function foo() external view returns (string memory) {
return string.concat(s1, " ", s2);
}
}
Run Code Online (Sandbox Code Playgroud)
文档:https://docs.soliditylang.org/en/v0.8.12/types.html#the-functions-bytes-concat-and-string-concat
| 归档时间: |
|
| 查看次数: |
2947 次 |
| 最近记录: |