您的位置:首页 > 其它

区块链学堂(22):String 数组类型

2018-01-05 09:18 267 查看
String类型的官方定义

String literals are written with either double or single-quotes (“foo” or ‘bar’). They do not imply trailing zeroes as in C; “foo”` represents three bytes not four. As with integer literals, their type can vary, but they are implicitly convertible to bytes1,
…, bytes32, if they fit, to bytes and to string.

String literals support escape characters, such as \n, \xNN and \uNNNN. \xNN takes a hex value and inserts the appropriate byte, while \uNNNN takes a Unicode codepoint and inserts an UTF-8 sequence. 引用自here


String数组类型定义

string[] strArr


String数组的增删改查

Step 1: 首先撰写一个String数组的contract和构造函数
pragma solidity 0.4.10;
contract Demo {
string[] public strArr;
function Demo() {
strArr.push("init");
}
}


Step 2: 添加一个Add function
function Add(string str) {
strArr.push(str);
}


Step 3: 添加一个Update function
function Update(uint index, string str) {
if (index > strArr.length-1) throw;
strArr[index] = str;
}


大家注意一下,如果index>strArr.length-1, 此时应抛出异常

Step 4: 添加一个ValueOf function
function ValueOf(uint index) returns (string str){
if (index > strArr.length-1) throw;
return strArr[index];
}


Step 5: 添加一个DeleteAt function
function DeleteAt(uint index) {
uint len = strArr.length;
if (index > len-1) throw;
for (uint i = index; i<len-1; i++) {
strArr[i] = strArr[i+1];
}

delete strArr[len-1];
strArr.length--;
}


原文:http://www.ethchinese.com/?p=1022

QQ群:559649971 (区块链学堂粉丝群)

个人微信:steven_k_colin



获取最新区块链咨询,请关注《以太中文网》微信公众号:以太中文网

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  以太坊