stringutils

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultPrecision      = 2
	DefaultBase           = 10
	DefaultFormat         = 'f'
	DefaultNilMapFormat   = "{}"
	DefaultNilSliceFormat = "[]"
	DefaultNilFormat      = "<nil>"
)

Default options.

Variables

This section is empty.

Functions

func Capitalize added in v1.8.0

func Capitalize(str string) string

Capitalize capitalizes the first letter of the given string. Returns the string unchanged if empty.

func Contains added in v1.9.0

func Contains(str, substr string, caseInsensitive bool) bool

Contains checks if string contains substring with optional case-insensitive matching.

func DefaultIfEmpty added in v1.9.0

func DefaultIfEmpty(str, def string) string

DefaultIfEmpty returns default if string is empty or only whitespace. Returns the trimmed string if not empty.

func EllipsisMiddle added in v1.9.0

func EllipsisMiddle(str string, maxLength int) string

EllipsisMiddle truncates middle of string with ellipsis if longer than maxLength. Keeps approximately equal parts from start and end.

func JoinNonEmpty added in v1.9.0

func JoinNonEmpty(strs []string, sep string) string

JoinNonEmpty joins strings with a separator, skipping empty strings.

func PadLeft added in v1.3.1

func PadLeft(str string, padWith string, padTo int) string

PadLeft - Pad a string to a certain length (in bytes) with another string on the left side.

func PadRight added in v1.3.1

func PadRight(str string, padWith string, padTo int) string

PadRight - Pad a string to a certain length (in bytes) with another string on the right side.

func RemoveWhitespace added in v1.9.0

func RemoveWhitespace(str string) string

RemoveWhitespace removes all whitespace from string.

func Reverse added in v1.9.0

func Reverse(str string) string

Reverse reverses a string (UTF-8 safe).

func SplitAndTrim added in v1.9.0

func SplitAndTrim(str, sep string) []string

SplitAndTrim splits a string by separator and trims whitespace from each part. Empty parts are removed from the result.

func Stringify

func Stringify(value any, opts ...Options) string

Stringify receives an arbitrary value and converts it into a string. Stringify also accepts an options object with the following properties:

Options.NilFormat: the string format for nil values, defaults to "<nil>".
Options.NilMapFormat: the string format for nil map objects, defaults to "{}".
Options.NilSliceFormat: the string format for nil slice objects, defaults to "[]".
Options.Base: a number between 2-36 as the base when converting ints and uints to strings, defaults to Base 10.
Options.Precision: number of digits to include when converting floats and complex numbers to strings, defaults to 2.
Options.Format: the number notation format, using the stdlib ftoa functionalities, defaults to 'f':
	'b' (-ddddp±ddd, a binary exponent),
	'e' (-d.dddde±dd, a decimal exponent),
	'E' (-d.ddddE±dd, a decimal exponent),
	'f' (-ddd.dddd, no exponent),
	'g' ('e' for large exponents, 'f' otherwise),
	'G' ('E' for large exponents, 'f' otherwise),
	'x' (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or
	'X' (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).

func ToCamelCase added in v1.9.0

func ToCamelCase(str string) string

ToCamelCase converts string to camelCase. Splits on spaces, hyphens, and underscores.

func ToKebabCase added in v1.9.0

func ToKebabCase(str string) string

ToKebabCase converts string to kebab-case.

func ToSnakeCase added in v1.9.0

func ToSnakeCase(str string) string

ToSnakeCase converts string to snake_case.

func Truncate added in v1.9.0

func Truncate(str string, maxLength int, suffix string) string

Truncate truncates a string to maxLength with optional suffix. If the string is longer than maxLength, it's cut and suffix is appended.

Types

type Options

type Options struct {
	Base           int
	Format         byte
	Precision      int
	NilFormat      string
	NilMapFormat   string
	NilSliceFormat string
}

Options - stringification options.

Jump to

Keyboard shortcuts

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