StringUtils

Git Source

Author: Synapse Contributors

Library for handling strings, that could be used with any Solidity version from 0.6 to 0.8.

State Variables

NOT_FOUND

The value returned by indexOf when the substring is not found.

uint256 internal constant NOT_FOUND = type(uint256).max;

ZERO

bytes1 private constant ZERO = bytes1("0");

NINE

bytes1 private constant NINE = bytes1("9");

Functions

length

Returns the length of a string

function length(string memory str) internal pure returns (uint256);

substring

Returns a substring of a string in the range [startIndex, endIndex)

function substring(string memory str, uint256 startIndex, uint256 endIndex) internal pure returns (string memory);

Parameters

NameTypeDescription
strstringThe string to take a substring of
startIndexuint256The start index (inclusive)
endIndexuint256The end index (exclusive)

suffix

Returns a suffix of a string starting at the given index, inclusive: [startIndex, str.length)

function suffix(string memory str, uint256 startIndex) internal pure returns (string memory);

Parameters

NameTypeDescription
strstringThe string to take a suffix of
startIndexuint256The start index (inclusive)

prefix

Returns a prefix of a string ending at the given index, exclusive: [0, endIndex)

function prefix(string memory str, uint256 endIndex) internal pure returns (string memory);

Parameters

NameTypeDescription
strstringThe string to take a prefix of
endIndexuint256The end index (exclusive)

concat

Concatenates two strings

function concat(string memory a, string memory b) internal pure returns (string memory);

concat

Concatenates three strings

function concat(string memory a, string memory b, string memory c) internal pure returns (string memory);

concat

Concatenates four strings

function concat(
    string memory a,
    string memory b,
    string memory c,
    string memory d
)
    internal
    pure
    returns (string memory);

concat

Concatenates five strings

function concat(
    string memory a,
    string memory b,
    string memory c,
    string memory d,
    string memory e
)
    internal
    pure
    returns (string memory);

concat

Concatenates six strings

function concat(
    string memory a,
    string memory b,
    string memory c,
    string memory d,
    string memory e,
    string memory f
)
    internal
    pure
    returns (string memory);

duplicate

Duplicates a string a given number of times. Example: duplicate("abc", 3) = "abcabcabc"

function duplicate(string memory str, uint256 times) internal pure returns (string memory duplicateStr);

equals

Checks if two strings are equal

function equals(string memory a, string memory b) internal pure returns (bool);

Parameters

NameTypeDescription
astringThe first string
bstringThe second string

indexOf

Returns the index of the first occurrence of a substring in a string, or NOT_FOUND if not found.

function indexOf(string memory str, string memory subStr) internal pure returns (uint256);

Parameters

NameTypeDescription
strstringThe string to search in
subStrstringThe substring to search for

lastIndexOf

Returns the index of the last occurrence of a substring in a string, or NOT_FOUND if not found.

function lastIndexOf(string memory str, string memory subStr) internal pure returns (uint256);

Parameters

NameTypeDescription
strstringThe string to search in
subStrstringThe substring to search for

toUint

Derives integer from its string representation.

function toUint(string memory str) internal pure returns (uint256 val);

Parameters

NameTypeDescription
strstringThe string to convert

fromUint

Converts an integer to its string representation.

function fromUint(uint256 val) internal pure returns (string memory);

fromFloat

Converts a float to its string representation.

function fromFloat(uint256 val, uint256 decimals) internal pure returns (string memory);

Parameters

NameTypeDescription
valuint256The float to convert, scaled by 10**decimals
decimalsuint256The number of decimals to use

fromWei

Converts a float to its string representation, using 18 decimals.

function fromWei(uint256 val) internal pure returns (string memory);

Parameters

NameTypeDescription
valuint256The float to convert, scaled by 10**18