Documentation ¶
Overview ¶
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 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
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AlignCenter ¶
AlignCenter centre-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 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 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 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 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.
Types ¶
This section is empty.