Skip to content
superduck-aiPublic

About

Idiomatic Go string utilities inspired by Apache Commons Lang

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-langx

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.

Install

The module requires Go 1.26.2 or later.

go get github.com/superduck-ai/go-langx@latest

Import the package directly:

import "github.com/superduck-ai/go-langx/stringutils"

Quick start

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...

Semantics

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.

API overview

  • Search and compare: IndexOf, LastIndexOf, ContainsIgnoreCase, CountMatches, Difference, and GetCommonPrefix.
  • Substrings and transforms: Left, Right, Mid, SubstringBetween, Reverse, Rotate, Abbreviate, and StripAccents.
  • Validation and defaults: IsBlank, IsAlpha, IsNumeric, DefaultIfBlank, and FirstNonEmpty.
  • Splitting, joining, and padding: SplitString, SplitPreserveAllTokens, JoinInts, LeftPad, and Center.
  • Similarity: Levenshtein, thresholded Levenshtein, Jaro-Winkler, and fuzzy distance.

See the package documentation for the complete API and edge-case behavior.

Development

go test ./...
go vet ./...

About

Idiomatic Go string utilities inspired by Apache Commons Lang

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages