stringutils

package module
v1.2.1 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

String manipulation:

  • Contains: checks if slice contains a string.
  • ContainsAnySubstring: checks if string contains any of provided substring.
  • DeDup: removes duplicates from slice of strings, optimized for performance, good for short slices only.
  • DeDupBig: removes duplicates from slice. Should be used instead of DeDup for large slices.
  • SliceToString: converts slice of any to a slice of strings.
  • HasCommonElement: checks if any element of the second slice is in the first slice.
  • 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.
  • Truncate: cuts string to the given length and adds ellipsis if it was truncated.
  • TruncateWords: cuts string to the given number of words and adds ellipsis if it was truncated.
  • 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. optimized for performance, good for short slices only!

func DeDupBig

func DeDupBig(keys []string) (result []string)

DeDupBig remove duplicates from slice. Should be used instead of DeDup for large slices

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 RandomWord added in v1.2.0

func RandomWord(minLen, maxLen int) string

RandomWord generates pronounceable random word with length between minLen and maxLen

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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