stringutils

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2025 License: MIT Imports: 4 Imported by: 2

README

stringutils Build Status Go Report Card Coverage Status

Package stringutils provides useful string operations.

Details

Slice Operations
  • Contains: checks if slice contains a string.
  • DeDup: removes duplicates from slice of strings while preserving order (stable).
  • DeDupBig: deprecated alias for DeDup, kept for backwards compatibility.
  • SliceToString: converts slice of any to a slice of strings.
  • Filter: returns a new slice containing only elements that match the predicate function.
  • Map: applies a transform function to each element and returns a new slice.
  • Reverse: returns a new slice with elements in reversed order.
  • IndexOf: returns the index of the first occurrence of element in slice, or -1 if not found.
  • LastIndexOf: returns the index of the last occurrence of element in slice, or -1 if not found.
Set Operations
  • HasCommonElement: checks if any element of the second slice is in the first slice.
  • Difference: returns elements that are in the first slice but not in the second.
  • Union: combines multiple slices and removes duplicates, preserving order.
  • Intersection: returns elements that are present in both slices, preserving order from first slice.
String Checking
  • ContainsAnySubstring: checks if string contains any of provided substrings.
  • HasPrefixSlice: checks if any string in the slice starts with the given prefix.
  • HasSuffixSlice: checks if any string in the slice ends with the given suffix.
  • IsBlank: returns true if string is empty or contains only whitespace.
String Manipulation
  • Truncate: cuts string to the given length (in runes) and adds ellipsis if it was truncated.
  • TruncateWords: cuts string to the given number of words and adds ellipsis if it was truncated.
  • NormalizeWhitespace: replaces multiple whitespace characters with single space and trims.
  • RemovePrefix: removes the prefix from string if present, otherwise returns unchanged.
  • RemoveSuffix: removes the suffix from string if present, otherwise returns unchanged.
String Generation
  • RandomWord: generates pronounceable random word with given min/max length.

Install and update

go get -u github.com/go-pkgz/stringutils

Usage examples

Documentation

Overview

Package stringutils provides utilities for working with strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(src string, inSlice []string) bool

Contains string in slice

func ContainsAnySubstring

func ContainsAnySubstring(s string, subStrings []string) bool

ContainsAnySubstring checks if string contains any of provided substring

func DeDup

func DeDup(keys []string) []string

DeDup remove duplicates from slice. This function is stable - it preserves the order of first occurrences.

func DeDupBig

func DeDupBig(keys []string) []string

DeDupBig remove duplicates from slice. Deprecated: Use DeDup instead. This function now just calls DeDup for backwards compatibility.

func Difference added in v1.3.0

func Difference(a, b []string) []string

Difference returns elements that are in the first slice but not in the second

func Filter added in v1.3.0

func Filter(slice []string, predicate func(string) bool) []string

Filter returns a new slice containing only elements that match the predicate

func HasCommonElement

func HasCommonElement(a, b []string) bool

HasCommonElement checks if any element of the second slice is in the first slice

func HasPrefixSlice added in v1.1.0

func HasPrefixSlice(prefix string, slice []string) bool

HasPrefixSlice checks if any string in the slice starts with the given prefix

func HasSuffixSlice added in v1.1.0

func HasSuffixSlice(suffix string, slice []string) bool

HasSuffixSlice checks if any string in the slice ends with the given suffix

func IndexOf added in v1.3.0

func IndexOf(slice []string, element string) int

IndexOf returns the index of the first occurrence of element in slice, or -1 if not found

func Intersection added in v1.3.0

func Intersection(a, b []string) []string

Intersection returns elements that are present in both slices, preserving order from first slice

func IsBlank added in v1.3.0

func IsBlank(s string) bool

IsBlank returns true if string is empty or contains only whitespace

func LastIndexOf added in v1.3.0

func LastIndexOf(slice []string, element string) int

LastIndexOf returns the index of the last occurrence of element in slice, or -1 if not found

func Map added in v1.3.0

func Map(slice []string, transform func(string) string) []string

Map applies transform function to each element and returns new slice

func NormalizeWhitespace added in v1.3.0

func NormalizeWhitespace(s string) string

NormalizeWhitespace replaces multiple whitespace characters with single space and trims

func RandomWord added in v1.2.0

func RandomWord(minLen, maxLen int) string

RandomWord generates pronounceable random word with length between minLen and maxLen

func RemovePrefix added in v1.3.0

func RemovePrefix(s, prefix string) string

RemovePrefix removes the prefix from s if present, otherwise returns s unchanged

func RemoveSuffix added in v1.3.0

func RemoveSuffix(s, suffix string) string

RemoveSuffix removes the suffix from s if present, otherwise returns s unchanged

func Reverse added in v1.3.0

func Reverse(slice []string) []string

Reverse returns a new slice with elements in reversed order

func SliceToString

func SliceToString(s []any) []string

SliceToString converts slice of any to slice of string

func Truncate added in v1.2.0

func Truncate(s string, maxLen int) string

Truncate cuts string to the given length (in runes) and adds ellipsis if it was truncated if maxLen is less than 4 (3 chars for ellipsis + 1 rune from string), returns empty string

func TruncateWords added in v1.2.0

func TruncateWords(s string, maxWords int) string

TruncateWords cuts string to the given number of words and adds ellipsis if it was truncated returns empty string if maxWords is 0

func Union added in v1.3.0

func Union(slices ...[]string) []string

Union combines multiple slices and removes duplicates, preserving order

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL