zstring

package
v0.0.0-...-9840c0c Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 4 Imported by: 21

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlignCenter

func AlignCenter(s string, n int) string

AlignCenter centre-aligns a string, filling up any remaining width with spaces.

func AlignLeft

func AlignLeft(s string, n int) string

AlignLeft left-aligns a string, filling up any remaining width with spaces.

func AlignRight

func AlignRight(s string, n int) string

AlignRight right-aligns a string, filling up any remaining width with spaces.

func ElideCenter

func ElideCenter(s string, n int) string

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

func ElideLeft(s string, n int) string

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

func ElideRight(s string, n int) string

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

func Fields(s, sep string) []string

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

func From(s string, sep string) string

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

func GetLine(in string, n int) string

GetLine gets the nth line \n-denoted line from a string.

Line indexing starts at 1.

func HasPrefixes

func HasPrefixes(s string, prefixes ...string) bool

HasPrefixes tests whether the string s starts with any of the prefixes.

Identical to:

strings.HasPrefix(s, "one") || strings.HasPrefix(s, "two")

func HasSuffixes

func HasSuffixes(s string, suffixes ...string) bool

HasSuffixes tests whether the string s ends with any of the suffixes.

Identical to:

strings.HasSuffix(s, "one") || strings.HasSuffix(s, "two")

func HasUpper

func HasUpper(s string) bool

HasUpper reports if s has at least one upper-case character.

func Indent

func Indent(s string, n int) string

Ident adds n spaces of indentation to every line.

func IndexAll

func IndexAll(s, find string) []int

IndexAll finds all occurrences of the string "find".

func IndexN

func IndexN(s, find string, n uint) int

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

func IndexPairs(str, start, end string) [][]int

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

func IsASCII(s string) bool

IsASCII reports if this string looks like it's plain 7-bit ASCII.

func LowerFirst

func LowerFirst(s string) string

LowerFirst transforms the first character to lower case, leaving the rest of the casing alone.

func ReplacePairs

func ReplacePairs(str, start, end string, f func(int, string) string) string

ReplacePairs replaces everything starting with start and ending with end with the return value of the callback.

func Reverse

func Reverse(s string) string

Reverse a string.

func Sub

func Sub(s string, start, end int) string

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

func TrimPrefixes(s string, prefixes ...string) string

TrimPrefixes returns s without the provided leading prefixes strings.

Identical to:

s = strings.TrimPrefix(s, "one")
s = strings.TrimPrefix(s, "two")

func TrimSuffixes

func TrimSuffixes(s string, suffixes ...string) string

TrimSuffixes returns s without the provided trailing suffixes strings.

Identical to:

s = strings.TrimSuffix(s, "one")
s = strings.TrimSuffix(s, "two")

func Unwrap

func Unwrap(s string) string

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

func UpperFirst(s string) string

UpperFirst transforms the first character to upper case, leaving the rest of the casing alone.

func Upto

func Upto(s string, sep string) string

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]
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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