【区块链】智能合约字符串拼接

智能合约字符串拼接


string是数组,solidity不支持动态扩容,只能写个for循环一个个加,此处采用的做法是转成bytes

function stringAdd(string a, string b) returns(string){
    bytes memory _a = bytes(a);
    bytes memory _b = bytes(b);
    bytes memory res = new bytes(_a.length + _b.length);
    for(uint i = 0;i < _a.length;i++)
        res[i] = _a[i];
    for(uint j = 0;j < _b.length;j++)
        res[_a.length+j] = _b[j];  
    return string(res);
}

你可能感兴趣的:(区块链,区块链学习笔记)