如何在结实程度上反转一根弦? [英] how to reverse a string in solidity?

查看:10
本文介绍了如何在结实程度上反转一根弦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这是我的代码,我想用实心反转一个字符串:

function reverseValue(string _base) internal returns(string){
        bytes memory _baseBytes = bytes(_base);

        string memory _tempValue = new string(_baseBytes.length);
        bytes memory _newValue = bytes(_tempValue);

        for(uint i=_baseBytes.length;i<=0;i--){
            _newValue[_baseBytes.length - i] = _baseBytes[i];
        }

        return string(_newValue);
    }

但唯一的结果是以下代码: 0:string : u0000u0000u0000u0000u0000u0000

我认为我的代码写对了,但我找不到问题所在……TnX帮助我:)

推荐答案

我找到了这个答案,这是正确的代码:

function reverseValue(string _base) internal returns(string){
        bytes memory _baseBytes = bytes(_base);
        assert(_baseBytes.length > 0);

        string memory _tempValue = new string(_baseBytes.length);
        bytes memory _newValue = bytes(_tempValue);


        for(uint i=0;i<_baseBytes.length;i++){
            _newValue[ _baseBytes.length - i - 1] = _baseBytes[i];
        }

        return string(_newValue);
    }

现在,此特定代码的结果为:

_base : "shahab" -> result : 0 : string : "bahahas"

这篇关于如何在结实程度上反转一根弦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆