gpt4 book ai didi

ethereum - 多行的 Solidity 长字符串常量

转载 作者:行者123 更新时间:2023-12-05 08:46:55 34 4
gpt4 key购买 nike

只是想知道是否有一种方法可以将长字符串拆分成多条线?我找不到任何类型的行继续字符,如果您尝试使用这样的两行,则会抛出编译错误。连接字符串似乎也很复杂

 string memory s = "This is a very long line of text which I would like to split over
several lines";

连接字符串似乎也很复杂。我是否只需要将很长的字符串放在一条很长的线上?

最佳答案

您可以将值拆分为多个单独的字符串文字,每个字符串文字占一行。

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";
}

文档: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));
}
}

关于ethereum - 多行的 Solidity 长字符串常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68697805/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com