solidityで文字列連結して返却
文字列連結はこちらを使わせていただきました。
https://qiita.com/onokatio/items/4d74229bd6015379e379
デリミタを入力禁止にして運用する必要があるね
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.18; contract SimpleStore { mapping(address => mapping(uint => string)) addressToIdToMsg; mapping(address => uint) idToStoreCnt; string delimiter = "/"; function setMsg(string _msg) public { uint cnt = idToStoreCnt[msg.sender]; addressToIdToMsg[msg.sender][cnt] = _msg; idToStoreCnt[msg.sender]++; } function getMyAllMsg() public view returns(string) { string memory ret; string memory buf; uint storeLen = idToStoreCnt[msg.sender]; for(uint i=0; i<storeLen; i++){ buf = addressToIdToMsg[msg.sender][i]; ret = _strConnect(ret, delimiter); ret = _strConnect(ret, buf); } return ret; } function _strConnect(string str1, string str2) private pure returns(string) { bytes memory strbyte1 = bytes(str1); bytes memory strbyte2 = bytes(str2); bytes memory str = new bytes(strbyte1.length + strbyte2.length); uint8 point = 0; for(uint8 j = 0; j < strbyte1.length;j++){ str[point] = strbyte1[j]; point++; } for(uint8 k = 0; k < strbyte2.length;k++){ str[point] = strbyte2[k]; point++; } return string(str); } } |