std / strings

strings

import "std:strings"

Provides string manipulation functions. Functions are organized by allocation behavior: zero-alloc functions return views into input strings, Builder functions write to a Builder for explicit memory ownership, and Split functions return list[string] arrays where the caller frees the array. Case conversion is ASCII-only.

View source on Codeberg →

Types

#
type Builder list[byte]

Provides efficient string construction. Builder is a type alias for list[byte] that the caller owns.

Character Classification (Zero-Alloc)

#
func IsSpace(b byte) bool

Returns true if b is ASCII whitespace.

#
func IsDigit(b byte) bool

Returns true if b is an ASCII digit (0-9).

#
func IsLetter(b byte) bool

Returns true if b is an ASCII letter.

#
func IsUpper(b byte) bool

Returns true if b is an ASCII uppercase letter.

#
func IsLower(b byte) bool

Returns true if b is an ASCII lowercase letter.

Character Conversion (Zero-Alloc)

#
func ToLowerByte(b byte) byte

Converts an ASCII uppercase letter to lowercase.

#
func ToUpperByte(b byte) byte

Converts an ASCII lowercase letter to uppercase.

#
func ToLowerRune(r rune) rune

Converts an ASCII uppercase letter (as rune) to lowercase.

#
func ToUpperRune(r rune) rune

Converts an ASCII lowercase letter (as rune) to uppercase.

Comparison (Zero-Alloc)

#
func Compare(a, b string) int

Returns -1, 0, or 1 for lexicographic comparison.

#
func EqualFold(a, b string) bool

Reports whether a and b are equal under ASCII case-folding.

#
func Contains(s, substr string) bool

Reports whether substr is within s.

#
func ContainsByte(s string, b byte) bool

Reports whether b is within s.

#
func ContainsAny(s, chars string) bool

Reports whether any character in chars is within s.

#
func HasPrefix(s, prefix string) bool

Reports whether s starts with prefix.

#
func HasSuffix(s, suffix string) bool

Reports whether s ends with suffix.

#
func Index(s, substr string) int

Returns the index of the first instance of substr, or -1.

#
func IndexByte(s string, b byte) int

Returns the index of the first instance of b, or -1.

#
func IndexAny(s, chars string) int

Returns the index of the first instance of any char in chars, or -1.

#
func LastIndex(s, substr string) int

Returns the index of the last instance of substr, or -1.

#
func LastIndexByte(s string, b byte) int

Returns the index of the last instance of b, or -1.

#
func Count(s, substr string) int

Returns the number of non-overlapping instances of substr in s.

Trimming (Zero-Alloc, return views)

#
func TrimSpace(s string) string

Returns s with leading and trailing ASCII whitespace removed.

#
func TrimPrefix(s, prefix string) string

Returns s without the leading prefix.

#
func TrimSuffix(s, suffix string) string

Returns s without the trailing suffix.

#
func Trim(s, cutset string) string

Returns s with leading and trailing characters in cutset removed.

#
func TrimLeft(s, cutset string) string

Returns s with leading characters in cutset removed.

#
func TrimRight(s, cutset string) string

Returns s with trailing characters in cutset removed.

Cutting (Zero-Alloc, return views)

#
func Cut(s, sep string) (string, string, bool)

Slices s around the first instance of sep.

#
func CutPrefix(s, prefix string) (string, bool)

Returns s without the leading prefix and whether it was found.

#
func CutSuffix(s, suffix string) (string, bool)

Returns s without the trailing suffix and whether it was found.

Splitting (Caller frees dynamic array)

#
func Split(s, sep string) list[string]

Slices s into all substrings separated by sep.

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

Slices s into at most n substrings separated by sep.

#
func Fields(s string) list[string]

Splits s around whitespace into a dynamic array.

Builder Core

#
func NewBuilder() Builder

Returns a new Builder with default capacity.

#
func NewBuilderSize(capacity int) Builder

Returns a new Builder with the specified initial capacity.

#
func Write(b Builder, data []byte) int

Appends data to the Builder's buffer.

#
func WriteByte(b Builder, c byte)

Appends byte c to the Builder's buffer.

#
func WriteString(b Builder, s string) int

Appends string s to the Builder's buffer.

#
func WriteRune(b Builder, r rune) int

Appends the UTF-8 encoding of rune r.

#
func Len(b Builder) int

Returns the number of bytes in the buffer.

#
func Cap(b Builder) int

Returns the capacity of the buffer.

#
func Reset(b Builder)

Resets the Builder to empty, retaining the buffer.

#
func String(b Builder) string

Returns the accumulated string (view into buffer).

#
func Bytes(b Builder) []byte

Returns the accumulated bytes as a slice.

#
func Free(b Builder)

Frees the Builder's internal buffer.

Builder String Construction

#
func ToLower(b Builder, s string)

Writes the ASCII lowercase of s to the Builder.

#
func ToUpper(b Builder, s string)

Writes the ASCII uppercase of s to the Builder.

#
func Repeat(b Builder, s string, count int)

Writes s repeated count times to the Builder.

#
func Join(b Builder, parts []string, sep string)

Writes parts joined by sep to the Builder.

#
func Replace(b Builder, s, old, repl string, n int)

Writes s with up to n replacements of old with repl.

#
func ReplaceAll(b Builder, s, old, repl string)

Writes s with all occurrences of old replaced by repl.