Navigate:
Solidity StringUtils
~$SOST0.0%

Solidity StringUtils: String manipulation library for Solidity

A gas-efficient string utility library for Solidity using slice abstractions for string manipulation operations.

LIVE RANKINGS • 03:09 AM • STEADY
OVERALL
#442
10
CRYPTO
#40
9
30 DAY RANKING TREND
ovr#442
·Crypt#40
STARS
1.2K
FORKS
377
7D STARS
0
7D FORKS
-1
Tags:
See Repo:
Share:

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.

Solidity StringUtils

1

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.

2

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.

3

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();
    }
}



[ EXPLORE MORE ]

Related Repositories

Discover similar tools and frameworks used by developers