多行的 Solidity 长字符串常量
Solidity long string constants over multiple lines
只是想知道是否有一种方法可以将长字符串拆分成多条线?我找不到任何类型的行继续字符,如果您尝试使用这样的两行,则会抛出编译错误。连接字符串似乎也很复杂
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));
}
}
只是想知道是否有一种方法可以将长字符串拆分成多条线?我找不到任何类型的行继续字符,如果您尝试使用这样的两行,则会抛出编译错误。连接字符串似乎也很复杂
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));
}
}