go-langx provides string operations that are useful in Go but are not
available directly in the standard library. It is inspired by Apache Commons
Lang's StringUtils, with an API adapted to Go strings, runes, and explicit
error handling.
The module currently contains one public package:
stringutils— searching, slicing, transforming, validating, splitting, joining, padding, and comparing strings, plus edit-distance algorithms.
Use Go's strings, unicode, and regexp packages first. go-langx does not
repeat operations already covered by the standard library.
The module requires Go 1.26.2 or later.
go get github.com/superduck-ai/go-langx@latestImport the package directly:
import "github.com/superduck-ai/go-langx/stringutils"package main
import (
"fmt"
"log"
"github.com/superduck-ai/go-langx/stringutils"
)
func main() {
fmt.Println(stringutils.NormalizeSpace(" Go\tis\nfun "))
fmt.Println(stringutils.IndexOf("Go🦆lang", "🦆"))
short, err := stringutils.Abbreviate("abcdefg", 6)
if err != nil {
log.Fatal(err)
}
fmt.Println(short)
}Output:
Go is fun
2
abc...
Search and substring functions that expose positions use rune indexes, not
UTF-8 byte offsets. For example, IndexOf("Go🦆lang", "🦆") returns 2.
Functions return ordinary Go values rather than modeling Java null. APIs
that need to distinguish an empty result use a boolean, and operations with an
invalid width, range, locale, or pattern return an error where appropriate.
RemoveAll, RemoveFirst, ReplaceAll, ReplaceFirst, RemovePattern, and
ReplacePattern interpret their pattern argument using Go's regexp syntax.
- Search and compare:
IndexOf,LastIndexOf,ContainsIgnoreCase,CountMatches,Difference, andGetCommonPrefix. - Substrings and transforms:
Left,Right,Mid,SubstringBetween,Reverse,Rotate,Abbreviate, andStripAccents. - Validation and defaults:
IsBlank,IsAlpha,IsNumeric,DefaultIfBlank, andFirstNonEmpty. - Splitting, joining, and padding:
SplitString,SplitPreserveAllTokens,JoinInts,LeftPad, andCenter. - Similarity: Levenshtein, thresholded Levenshtein, Jaro-Winkler, and fuzzy distance.
See the package documentation for the complete API and edge-case behavior.
go test ./...
go vet ./...