stringx

April 13, 2026 ยท View on GitHub

sink-logo

stringx

stringx provides small helpers for common string operations.

It is designed to help you replace repetitive string handling code with small functions for:

  • checking whether a string is blank
  • returning the first non-empty string from a list of candidates
  • trimming a string to a maximum length
  • wrapping a string with a prefix and suffix
  • padding a string on the left or right to a target length

Overview

Use stringx when you want string manipulation code to be more concise and readable.

It is especially useful when:

  • you need a fallback string value from several candidates
  • you are formatting output that requires fixed-width columns or aligned text
  • you want to guard against blank user input without writing strings.TrimSpace inline

When to use it

Use stringx when:

  • a small helper makes the intent of a string operation explicit
  • the same string handling pattern appears in multiple places

Prefer standard library calls when:

  • the operation is a single strings function call that already reads clearly
  • the surrounding code already makes the intent obvious

API reference

Blank checks

FunctionPurpose
IsBlankReturns true if the string is empty or contains only whitespace

Fallback selection

FunctionPurpose
CoalesceReturns the first non-empty string from the provided values
CoalesceFuncReturns the first string satisfying a caller-provided predicate

Length limiting

FunctionPurpose
TruncateReturns the string trimmed to a maximum length (Unicode-safe)

Formatting

FunctionPurpose
WrapSurrounds the string with a given prefix and suffix
PadLeftLeft-pads the string with a character to the specified length
PadRightRight-pads the string with a character to the specified length

Notes

  • Truncate operates on runes, not bytes, so it is safe for multi-byte Unicode characters.
  • PadLeft and PadRight also operate on runes, so padding counts by character rather than byte.
  • Coalesce returns an empty string if all provided values are empty.
  • CoalesceFunc is useful when the definition of "non-empty" is caller-defined โ€” for example, skipping blank strings rather than just empty ones.

Examples