// SPDX-License-Identifier: MIT
pragma solidity >=0.6.12;
contract Test {
function stringToUint(string memory s) public pure returns(uint) {
bytes memory b = bytes(s);
uint result = 0;
for(uint i = 0; i < b.length; i++) {
if(uint8(b[i]) >= 48 && uint8(b[i]) <= 57) {
result = result * 10 + (uint8(b[i]) - 48);
}
}
return result;
}
}
来源:oschina
链接:https://my.oschina.net/Rhynie/blog/4943905