Skip to content

std/strings

import "github.com/eugenioenko/goalchemy/std/strings"

Package strings manipulates UTF-8 encoded strings.

func Compare(a, b string) int

Compare returns 0 if a == b, -1 if a < b, and +1 if a > b.

func Contains(s, substr string) bool

Contains reports whether substr is within s.

func ContainsAny(s, chars string) bool

ContainsAny reports whether any rune of chars is within s.

func ContainsFunc(s string, f func(rune) bool) bool

ContainsFunc reports whether any rune of s satisfies f.

func ContainsRune(s string, r rune) bool

ContainsRune reports whether r is within s.

func Count(s, substr string) int

Count counts the non-overlapping instances of substr in s. An empty substr counts as one more than the number of runes in s.

func Cut(s, sep string) (before, after string, found bool)

Cut slices s around the first instance of sep.

func CutPrefix(s, prefix string) (after string, found bool)

CutPrefix returns s without prefix and whether it was present.

func CutSuffix(s, suffix string) (before string, found bool)

CutSuffix returns s without suffix and whether it was present.

func EqualFold(s, t string) bool

EqualFold reports whether s and t are equal under simple Unicode case folding.

func Fields(s string) []string

Fields splits s around runs of white space.

func FieldsFunc(s string, f func(rune) bool) []string

FieldsFunc splits s around runs of runes satisfying f.

func HasPrefix(s, prefix string) bool

HasPrefix reports whether s begins with prefix.

func HasSuffix(s, suffix string) bool

HasSuffix reports whether s ends with suffix.

func Index(s, substr string) int

Index returns the index of the first instance of substr in s, or -1.

func IndexAny(s, chars string) int

IndexAny returns the index of the first rune of s that is in chars, or -1.

func IndexByte(s string, c byte) int

IndexByte returns the index of the first instance of c in s, or -1.

func IndexFunc(s string, f func(rune) bool) int

IndexFunc returns the index of the first rune satisfying f, or -1.

func IndexRune(s string, r rune) int

IndexRune returns the index of the first instance of r in s, or -1. For utf8.RuneError it matches the first invalid byte or encoded U+FFFD.

func Join(elems []string, sep string) string

Join concatenates elems with sep between them.

func LastIndex(s, substr string) int

LastIndex returns the index of the last instance of substr in s, or -1.

func LastIndexAny(s, chars string) int

LastIndexAny returns the index of the last rune of s that is in chars, or -1.

func LastIndexByte(s string, c byte) int

LastIndexByte returns the index of the last instance of c in s, or -1.

func LastIndexFunc(s string, f func(rune) bool) int

LastIndexFunc returns the index of the last rune satisfying f, or -1.

func Map(mapping func(rune) rune, s string) string

Map returns s with every rune mapped by mapping; runes mapped to a negative value are dropped. Invalid UTF-8 bytes are mapped as RuneError.

func Repeat(s string, count int) string

Repeat returns count copies of s. It panics if count is negative.

func Replace(s, old, new string, n int) string

Replace returns s with the first n non-overlapping instances of old replaced by new; n < 0 replaces all. An empty old matches before every rune and at the end.

func ReplaceAll(s, old, new string) string

ReplaceAll replaces every non-overlapping instance of old with new.

func Split(s, sep string) []string

Split slices s into all substrings separated by sep.

func SplitAfter(s, sep string) []string

SplitAfter slices s after each instance of sep.

func SplitAfterN(s, sep string, n int) []string

SplitAfterN slices s after each instance of sep into at most n substrings.

func SplitN(s, sep string, n int) []string

SplitN slices s into at most n substrings separated by sep; n < 0 means all.

func ToLower(s string) string

ToLower maps every rune of s to lower case.

func ToTitle(s string) string

ToTitle maps every rune of s to title case.

func ToUpper(s string) string

ToUpper maps every rune of s to upper case.

func ToValidUTF8(s, replacement string) string

ToValidUTF8 replaces each run of invalid UTF-8 bytes with replacement.

func Trim(s, cutset string) string

Trim removes leading and trailing runes contained in cutset.

func TrimFunc(s string, f func(rune) bool) string

TrimFunc removes leading and trailing runes satisfying f.

func TrimLeft(s, cutset string) string

TrimLeft removes leading runes contained in cutset.

func TrimLeftFunc(s string, f func(rune) bool) string

TrimLeftFunc removes leading runes satisfying f.

func TrimPrefix(s, prefix string) string

TrimPrefix removes prefix from s if present.

func TrimRight(s, cutset string) string

TrimRight removes trailing runes contained in cutset.

func TrimRightFunc(s string, f func(rune) bool) string

TrimRightFunc removes trailing runes satisfying f.

func TrimSpace(s string) string

TrimSpace removes leading and trailing white space.

func TrimSuffix(s, suffix string) string

TrimSuffix removes suffix from s if present.

type Builder struct {
}

Builder builds a string with appends.

func (b *Builder) Cap() int

Cap returns the capacity of the builder’s buffer.

func (b *Builder) Grow(n int)

Grow ensures space for another n bytes.

func (b *Builder) Len() int

Len returns the number of accumulated bytes.

func (b *Builder) Reset()

Reset empties the builder.

func (b *Builder) String() string

String returns the accumulated string.

func (b *Builder) Write(p []byte) (int, error)

Write appends p and always returns len(p), nil.

func (b *Builder) WriteByte(c byte) error

WriteByte appends c and always returns nil.

func (b *Builder) WriteRune(r rune) (int, error)

WriteRune appends the UTF-8 encoding of r and returns its length.

func (b *Builder) WriteString(s string) (int, error)

WriteString appends s and always returns len(s), nil.

type Reader struct {
}

Reader implements io.Reader, io.ReaderAt, io.Seeker, io.WriterTo, io.ByteScanner and io.RuneScanner over a string. The zero value reads nothing.

func NewReader(s string) *Reader

NewReader returns a Reader reading from s.

func (r *Reader) Len() int

Len returns the number of unread bytes.

func (r *Reader) Read(b []byte) (n int, err error)

Read implements io.Reader.

func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)

ReadAt implements io.ReaderAt. It does not change the read position.

func (r *Reader) ReadByte() (byte, error)

ReadByte implements io.ByteReader.

func (r *Reader) ReadRune() (ch rune, size int, err error)

ReadRune implements io.RuneReader.

func (r *Reader) Reset(s string)

Reset resets the Reader to read from s.

func (r *Reader) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker. Seeking past the end is allowed.

func (r *Reader) Size() int64

Size returns the original length of the underlying string.

func (r *Reader) UnreadByte() error

UnreadByte steps back one byte.

func (r *Reader) UnreadRune() error

UnreadRune steps back over the rune returned by the previous ReadRune.

func (r *Reader) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements io.WriterTo, writing the unread data to w.

type Replacer struct {
}

Replacer replaces a list of strings with replacements.

func NewReplacer(oldnew ...string) *Replacer

NewReplacer returns a Replacer from old, new string pairs. Replacements happen in target string order without overlapping; at the same position, earlier pairs win. It panics on an odd argument count.

func (r *Replacer) Replace(s string) string

Replace returns s with all replacements performed.