Last active
October 21, 2018 17:14
-
-
Save jchung05/01e6c497531284286a04368e9707c2d7 to your computer and use it in GitHub Desktop.
Basic implementations of stdlib string functions in Solidity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.24; | |
contract String { | |
using strings for *; | |
string public s; | |
function strlen(s1 string) public returns(uint) { | |
return byte(s1).length; | |
} | |
function strcmp(s1 string, s2 string) public returns(bool) { | |
return keccak256(s1) == keccak256(s2); | |
} | |
function strjoin(s1 string, s2 string) public returns(string) { | |
return abi.encodePacked(s1,s2); | |
} | |
function strupper(s1 string) public returns(string) { | |
for (uint i = 0; i < bytes(s1).length; i++) { | |
if (s1[i] >= 65 && s1[i] <= 90) { | |
s1[i] += 32; | |
} | |
} | |
return s1; | |
} | |
function strlower(s1 string) public returns(string) { | |
for (uint i = 0; i < bytes(s1).length; i++) { | |
if (s1[i] >= 97 && s1[i] <= 122) { | |
s1[i] -= 32; | |
} | |
} | |
return s1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment