【问题标题】:How do you compare strings in Solidity?你如何比较 Solidity 中的字符串?
【发布时间】:2023-03-09 14:29:01
【问题描述】:

我认为比较字符串会像这样做一样简单:

function withStrs(string memory a, string memory b) internal {
  if (a == b) {
    // do something
  }
}

但是这样做会给我一个错误Operator == not compatible with types string memory and string memory

什么是正确的方法?

【问题讨论】:

    标签: ethereum solidity


    【解决方案1】:

    您可以通过对字符串的打包编码值进行哈希处理来比较字符串:

    if (keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b))) {
      // do something
    }
    

    keccak256 是一个散列函数supported by Solidityabi.encodePacked() 通过the Application Binary Interface 编码值。

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 2013-05-04
      • 2010-09-16
      • 2018-06-14
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多