Solidity StringUtils: String manipulation library for Solidity
A gas-efficient string utility library for Solidity using slice abstractions for string manipulation operations.
Learn more about Solidity StringUtils
Solidity StringUtils is a string manipulation library for Solidity smart contracts that provides comprehensive text processing capabilities. The library implements functionality using a 'slice' abstraction that represents parts of strings with only an offset and length, making operations more gas-efficient than copying entire strings. Most functions modify slices in-place rather than allocating new ones to minimize gas costs, though copy operations are available when needed. The library supports common string operations like splitting, searching, concatenation, prefix/suffix matching, and Unicode handling for smart contract development.
Slice Abstraction
Uses lightweight slice objects that reference string parts with offset and length rather than copying data. This approach significantly reduces gas costs for string manipulation operations.
In-Place Modification
Most functions modify the original slice instead of allocating new ones to minimize memory usage. Copy operations are available when preservation of original data is required.
Unicode Support
Handles Unicode characters properly with rune-based operations and codepoint extraction. Provides accurate character counting and manipulation for international text.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "github.com/Arachnid/solidity-stringutils/strings.sol";
contract StringProcessor {
using strings for *;
function splitDomain(string memory url) public pure returns (string[] memory) {
strings.slice memory s = url.toSlice();
strings.slice memory delim = ".".toSlice();
// Count parts to allocate array
string[] memory parts = new string[](s.count(delim) + 1);
// Split and store each part
for(uint i = 0; i < parts.length; i++) {
parts[i] = s.split(delim).toString();
}
return parts;
}
function getFileExtension(string memory filename) public pure returns (string memory) {
strings.slice memory s = filename.toSlice();
strings.slice memory dot = ".".toSlice();
if (s.count(dot) == 0) {
return "";
}
// Find last occurrence and get extension
s.rfind(dot);
return s.beyond(dot).toString();
}
}Related Repositories
Discover similar tools and frameworks used by developers
Dogecoin Core
Open-source software for Dogecoin blockchain nodes with wallet and network participation.
Ethers.js
A TypeScript library for Ethereum blockchain interaction, providing wallet management, smart contract interfaces, and provider connections.
Mempool
Open-source Bitcoin mempool visualizer and blockchain explorer with transaction monitoring and fee estimation.
LND
Complete Lightning Network node implementation with channel management, payment routing, and BOLT specification compliance.
Zcash
Full node implementation of Zcash cryptocurrency with zero-knowledge privacy features and shielded transactions.