Help us shape the future of Go! Take the 2026 Go Developer Survey

strings

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package strings manipulates UTF-8 encoded strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare(a, b string) int

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

func Contains

func Contains(s, substr string) bool

Contains reports whether substr is within s.

func ContainsAny

func ContainsAny(s, chars string) bool

ContainsAny reports whether any rune of chars is within s.

func ContainsFunc

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

ContainsFunc reports whether any rune of s satisfies f.

func ContainsRune

func ContainsRune(s string, r rune) bool

ContainsRune reports whether r is within s.

func Count

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

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

Cut slices s around the first instance of sep.

func CutPrefix

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

CutPrefix returns s without prefix and whether it was present.

func CutSuffix

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

CutSuffix returns s without suffix and whether it was present.

func EqualFold

func EqualFold(s, t string) bool

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

func Fields

func Fields(s string) []string

Fields splits s around runs of white space.

func FieldsFunc

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

FieldsFunc splits s around runs of runes satisfying f.

func HasPrefix

func HasPrefix(s, prefix string) bool

HasPrefix reports whether s begins with prefix.

func HasSuffix

func HasSuffix(s, suffix string) bool

HasSuffix reports whether s ends with suffix.

func Index

func Index(s, substr string) int

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

func IndexAny

func IndexAny(s, chars string) int

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

func IndexByte

func IndexByte(s string, c byte) int

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

func IndexFunc

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

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

func IndexRune

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

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

Join concatenates elems with sep between them.

func LastIndex

func LastIndex(s, substr string) int

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

func LastIndexAny

func LastIndexAny(s, chars string) int

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

func LastIndexByte

func LastIndexByte(s string, c byte) int

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

func LastIndexFunc

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

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

func Map

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

func Repeat(s string, count int) string

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

func Replace

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

func ReplaceAll(s, old, new string) string

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

func Split

func Split(s, sep string) []string

Split slices s into all substrings separated by sep.

func SplitAfter

func SplitAfter(s, sep string) []string

SplitAfter slices s after each instance of sep.

func SplitAfterN

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

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

func SplitN

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

func ToLower(s string) string

ToLower maps every rune of s to lower case.

func ToTitle

func ToTitle(s string) string

ToTitle maps every rune of s to title case.

func ToUpper

func ToUpper(s string) string

ToUpper maps every rune of s to upper case.

func ToValidUTF8

func ToValidUTF8(s, replacement string) string

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

func Trim

func Trim(s, cutset string) string

Trim removes leading and trailing runes contained in cutset.

func TrimFunc

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

TrimFunc removes leading and trailing runes satisfying f.

func TrimLeft

func TrimLeft(s, cutset string) string

TrimLeft removes leading runes contained in cutset.

func TrimLeftFunc

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

TrimLeftFunc removes leading runes satisfying f.

func TrimPrefix

func TrimPrefix(s, prefix string) string

TrimPrefix removes prefix from s if present.

func TrimRight

func TrimRight(s, cutset string) string

TrimRight removes trailing runes contained in cutset.

func TrimRightFunc

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

TrimRightFunc removes trailing runes satisfying f.

func TrimSpace

func TrimSpace(s string) string

TrimSpace removes leading and trailing white space.

func TrimSuffix

func TrimSuffix(s, suffix string) string

TrimSuffix removes suffix from s if present.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder builds a string with appends.

func (*Builder) Cap

func (b *Builder) Cap() int

Cap returns the capacity of the builder's buffer.

func (*Builder) Grow

func (b *Builder) Grow(n int)

Grow ensures space for another n bytes.

func (*Builder) Len

func (b *Builder) Len() int

Len returns the number of accumulated bytes.

func (*Builder) Reset

func (b *Builder) Reset()

Reset empties the builder.

func (*Builder) String

func (b *Builder) String() string

String returns the accumulated string.

func (*Builder) Write

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

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

func (*Builder) WriteByte

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

WriteByte appends c and always returns nil.

func (*Builder) WriteRune

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

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

func (*Builder) WriteString

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

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

type Reader added in v0.6.0

type Reader struct {
	// contains filtered or unexported fields
}

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 added in v0.6.0

func NewReader(s string) *Reader

NewReader returns a Reader reading from s.

func (*Reader) Len added in v0.6.0

func (r *Reader) Len() int

Len returns the number of unread bytes.

func (*Reader) Read added in v0.6.0

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

Read implements io.Reader.

func (*Reader) ReadAt added in v0.6.0

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

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

func (*Reader) ReadByte added in v0.6.0

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

ReadByte implements io.ByteReader.

func (*Reader) ReadRune added in v0.6.0

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

ReadRune implements io.RuneReader.

func (*Reader) Reset added in v0.6.0

func (r *Reader) Reset(s string)

Reset resets the Reader to read from s.

func (*Reader) Seek added in v0.6.0

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

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

func (*Reader) Size added in v0.6.0

func (r *Reader) Size() int64

Size returns the original length of the underlying string.

func (*Reader) UnreadByte added in v0.6.0

func (r *Reader) UnreadByte() error

UnreadByte steps back one byte.

func (*Reader) UnreadRune added in v0.6.0

func (r *Reader) UnreadRune() error

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

func (*Reader) WriteTo added in v0.6.0

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

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

type Replacer

type Replacer struct {
	// contains filtered or unexported fields
}

Replacer replaces a list of strings with replacements.

func NewReplacer

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 (*Replacer) Replace

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

Replace returns s with all replacements performed.

Jump to

Keyboard shortcuts

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