StringUtils
Library that simplifies working with strings in Solidity
char
function char(string _s, uint256 _index) public pure returns (string)
Get character in string by index
Parameters
| Name |
Type |
Description |
| _s |
string |
Input string |
| _index |
uint256 |
Target index in the string |
Return Values
| Name |
Type |
Description |
| [0] |
string |
Character by index |
equal
function equal(string _s1, string _s2) internal pure returns (bool)
Compares two strings
Parameters
| Name |
Type |
Description |
| _s1 |
string |
One string |
| _s2 |
string |
Another string |
Return Values
| Name |
Type |
Description |
| [0] |
bool |
Are string equal |
length
function length(string _s) internal pure returns (uint256)
Gets length of the string
Parameters
| Name |
Type |
Description |
| _s |
string |
Input string |
Return Values
| Name |
Type |
Description |
| [0] |
uint256 |
The lenght of the string |
concat
function concat(string _s1, string _s2) internal pure returns (string)
Concats two strings
Parameters
| Name |
Type |
Description |
| _s1 |
string |
One string |
| _s2 |
string |
Another string |
Return Values
| Name |
Type |
Description |
| [0] |
string |
The concatenation of the strings |
substr
function substr(string _str, uint256 _start, uint256 _end) public pure returns (string)
Creates a substring from a string
Ex. substr('0x9A9f2CCfdE556A7E9Ff0848998Aa4a0CFD8863AE', 2, 42) => '9A9f2CCfdE556A7E9Ff0848998Aa4a0CFD8863AE'
Parameters
| Name |
Type |
Description |
| _str |
string |
Input string |
| _start |
uint256 |
Start index (inclusive) |
| _end |
uint256 |
End index (not inclusive) |
Return Values
| Name |
Type |
Description |
| [0] |
string |
Substring |
isIn
function isIn(string _char, string _string) public pure returns (bool)
Checks is _char is present in the _string
Ex. _.in('123_456') => true
Ex. 8.in('123456') => false
Parameters
| Name |
Type |
Description |
| _char |
string |
Searched character |
| _string |
string |
String to search in |
Return Values
| Name |
Type |
Description |
| [0] |
bool |
Is the character presented in the string |
fromHex
function fromHex(string s) public pure returns (bytes)
toString
function toString(uint256 _num) internal pure returns (string)
Inspired by OraclizeAPI's implementation - MIT licence
https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
Converts a uint256 to its ASCII string decimal representation
Parameters
| Name |
Type |
Description |
| _num |
uint256 |
Input number |
Return Values
| Name |
Type |
Description |
| [0] |
string |
Number represented as a string |
toUint256
function toUint256(string _s) public pure returns (uint256 value)
Converts a decimal number (provided as a string) to uint256
Parameters
| Name |
Type |
Description |
| _s |
string |
Input decimal number (provided as a string) |
Return Values
| Name |
Type |
Description |
| value |
uint256 |
Unsigned integer from input string |
parseScientificNotation
function parseScientificNotation(string _s) public pure returns (string result)
Converts a decimal number (provided as a string) with e symbol (1e18) to number (returned as a string)
Parameters
| Name |
Type |
Description |
| _s |
string |
Input decimal number (provided as a string) |
Return Values
| Name |
Type |
Description |
| result |
string |
Unsigned integer in a string format |
mayBeNumber
function mayBeNumber(string _string) public pure returns (bool)
If the string starts with a number, so we assume that it's a number.
Parameters
| Name |
Type |
Description |
| _string |
string |
is a current string for checking |
Return Values
| Name |
Type |
Description |
| [0] |
bool |
isNumber that is true if the string starts with a number, otherwise is false |
mayBeAddress
function mayBeAddress(string _string) public pure returns (bool)
If the string starts with 0x symbols, so we assume that it's an address.
Parameters
| Name |
Type |
Description |
| _string |
string |
is a current string for checking |
Return Values
| Name |
Type |
Description |
| [0] |
bool |
isAddress that is true if the string starts with 0x symbols, otherwise is false |
isValidVarName
function isValidVarName(string _s) public pure returns (bool)
Checks is string is a valid DSL variable name (matches regexp /^([A-Z$][A-Z\d_$]*)$/g)_
Parameters
| Name |
Type |
Description |
| _s |
string |
is a current string to check |
Return Values
| Name |
Type |
Description |
| [0] |
bool |
isCapital whether the string is a valid DSL variable name or not |