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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值