想知道是否可以在字符串内存和字符串 calldata 之间进行转换,以便使用仅适用于字符串 calldata 的 string[start : end] 形式的索引。这个功能似乎有效:
function splice(string calldata source, int startPos, int numchars) public pure returns(string memory) {
if (startPos > int(length(sourcestring))) return "";
int start = startPos -1;
int end = startPos + (numchars -1);
string memory retval = string(source[uint(start) : uint(end)]);
return retval;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我将参数更改source
为字符串内存,则会出现错误
,因为显然获取子字符串的string memory retval = string(source([uint(start) : uint(end)])
形式适用于字符串而不是字符串,并且没有明显的方法将 a 转换为 a 。sourcestring[start : end]
calldata
memory
string memory
string calldata
有什么办法可以做到这一点吗?
归档时间: |
|
查看次数: |
2578 次 |
最近记录: |