您当前的位置: 首页 >  区块链
  • 0浏览

    0关注

    1477博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

区块链 以太坊 solidity 如何比较2个字符串相等

软件工程小施同学 发布时间:2022-02-15 09:18:24 ,浏览量:0

不能直接return a == b;,因为Solidity是不支持两个字符串直接比较的。

将string类型转换为bytes类型,它实际上是一个字节数组,每一个字节是可以直接比较,因此只要所有的字节都能相等,就代表两个字符串相等。

function isEqual(string memory a, string memory b) public pure returns (bool) {
        bytes memory aa = bytes(a);
        bytes memory bb = bytes(b);
        // 如果长度不等,直接返回
        if (aa.length != bb.length) return false;
        // 按位比较
        for(uint i = 0; i < aa.length; i ++) {
            if(aa[i] != bb[i]) return false;
        }

        return true;
}

https://github.com/WeBankBlockchain/SmartDev-Contract/blob/master/docs/miscs/tutorial/Solidity-basic.md

关注
打赏
1665320866
查看更多评论
立即登录/注册

微信扫码登录

0.0411s