package zstring
import "zgo.at/zstd/zstring"
Package zstring implements functions for strings.
All functions work correctly on Unicode codepoints/runes, but usually *don't* work on unicode clusters. That is, things like emojis composed of multiple codepoints and combining characters aren't dealt with unless explicitly mentioned otherwise.
Index ¶
- func AlignCenter(s string, n int) string
- func AlignLeft(s string, n int) string
- func AlignRight(s string, n int) string
- func ElideCenter(s string, n int) string
- func ElideLeft(s string, n int) string
- func ElideRight(s string, n int) string
- func Fields(s, sep string) []string
- func From(s string, sep string) string
- func GetLine(in string, n int) string
- func HasPrefixes(s string, prefixes ...string) bool
- func HasSuffixes(s string, suffixes ...string) bool
- func HasUpper(s string) bool
- func Indent(s string, n int) string
- func IndexAll(s, find string) []int
- func IndexN(s, find string, n uint) int
- func IndexPairs(str, start, end string) [][]int
- func IsASCII(s string) bool
- func LowerFirst(s string) string
- func ReplacePairs(str, start, end string, f func(int, string) string) string
- func Reverse(s string) string
- func Safe(s string) string
- func Sub(s string, start, end int) string
- func TrimPrefixes(s string, prefixes ...string) string
- func TrimSuffixes(s string, suffixes ...string) string
- func Unwrap(s string) string
- func UpperFirst(s string) string
- func Upto(s string, sep string) string
Functions ¶
func AlignCenter ¶
AlignCenter centre-aligns a string, filling up any remaining width with spaces.
func AlignLeft ¶
AlignLeft left-aligns a string, filling up any remaining width with spaces.
func AlignRight ¶
AlignRight right-aligns a string, filling up any remaining width with spaces.
func ElideCenter ¶
ElideCenter returns the "n" characters of the string.
If the string is shorter than "n" it will return the first n/2 characters and last n/2 characters of the string with "…" inserted in the centre. Otherwise the entire string is returned as-is.
func ElideLeft ¶
ElideLeft returns the "n" left characters of the string.
If the string is shorter than "n" it will return the first "n" characters of the string with "…" appended. Otherwise the entire string is returned as-is.
func ElideRight ¶
ElideRight returns the "n" right characters of the string.
If the string is shorter than "n" it will return the first "n" characters of the string with "…" appended. Otherwise the entire string is returned as-is.
func Fields ¶
Fields slices s to all substrings separated by sep. Leading/trailing whitespace and empty elements will be removed.
e.g. "a;b", "a; b", " a ; b", and "a; b;" will all result in ["a", "b"].
func From ¶
From slices the string from first occurrence of sep. This is a shortcut for:
if i := strings.Index(s, sep); i > -1 { s = s[i+len(sep):] }
func GetLine ¶
GetLine gets the nth line \n-denoted line from a string.
Line indexing starts at 1.
func HasPrefixes ¶
HasPrefixes tests whether the string s starts with any of the prefixes.
Identical to:
strings.HasPrefix(s, "one") || strings.HasPrefix(s, "two")
func HasSuffixes ¶
HasSuffixes tests whether the string s ends with any of the suffixes.
Identical to:
strings.HasSuffix(s, "one") || strings.HasSuffix(s, "two")
func HasUpper ¶
HasUpper reports if s has at least one upper-case character.
func Indent ¶
Ident adds n spaces of indentation to every line.
func IndexAll ¶
IndexAll finds all occurrences of the string "find".
func IndexN ¶
IndexN finds the nth occurrence of a string.
n starts at 1; returns -1 if there is no nth occurrence of this string.
func IndexPairs ¶
IndexPairs finds the position of all start/end pairs.
Nested pairs are not supported.
The return value is from last match to first match; this makes it easier to manipulate the string based on the indexes.
func IsASCII ¶
IsASCII reports if this string looks like it's plain 7-bit ASCII.
func LowerFirst ¶
LowerFirst transforms the first character to lower case, leaving the rest of the casing alone.
func ReplacePairs ¶
ReplacePairs replaces everything starting with start and ending with end with the return value of the callback.
func Reverse ¶
Reverse a string.
func Safe ¶
Safe converts a string to a "safe" value by replacing every non-letter and non-digit by a '-', squeezing consecutive dashes and trimming dashes at the start and end. Capitalisation is left intact.
If the result is an empty string, it will return a small random string instead of an empty string.
This makes the string suitable for pathnames, URLs, etc. This is more conservative than is strictly needed, but usually that's fine.
func Sub ¶
Sub returns a substring starting at start and ending at end.
Unlike regular string slicing this operates on runes/UTF-8 codepoints, rather than bytes.
func TrimPrefixes ¶
TrimPrefixes returns s without the provided leading prefixes strings.
Identical to:
s = strings.TrimPrefix(s, "one") s = strings.TrimPrefix(s, "two")
func TrimSuffixes ¶
TrimSuffixes returns s without the provided trailing suffixes strings.
Identical to:
s = strings.TrimSuffix(s, "one") s = strings.TrimSuffix(s, "two")
func Unwrap ¶
Unwrap a string: single newlines become a space, whereas two or more are preserved.
Removes newlines at the start and end of the string, but leaves all other spacing intact (including before and after newlines).
func UpperFirst ¶
UpperFirst transforms the first character to upper case, leaving the rest of the casing alone.
func Upto ¶
Upto slices the string up to the first occurrence of sep. This is a shortcut for:
if i := strings.Index(s, sep); i > -1 { s = s[:i] }
Source Files ¶
zstring.go
- Version
- v0.0.0-20240930202209-a63c3335042a (latest)
- Published
- Sep 30, 2024
- Platform
- linux/amd64
- Imports
- 5 packages
- Last checked
- 2 days ago –
Tools for package owners.