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

stringutil

package
v0.35.18 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Unlicense Imports: 3 Imported by: 19

Documentation

Overview

Package stringutil contains utilities for dealing with strings.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneSliceOrEmpty

func CloneSliceOrEmpty(strs []string) (clone []string)

CloneSliceOrEmpty returns the copy of strs or empty strings slice if strs is a nil slice.

Example
package main

import (
	"fmt"

	"github.com/AdguardTeam/golibs/stringutil"
)

func main() {
	var a, b []string

	b = stringutil.CloneSliceOrEmpty(a)
	fmt.Printf("b == nil is %t, len(b) is %d\n", b == nil, len(b))

	a = []string{}
	b = stringutil.CloneSliceOrEmpty(a)
	fmt.Printf("b == nil is %t, len(b) is %d\n", b == nil, len(b))

	a = []string{"a", "b", "c"}
	b = stringutil.CloneSliceOrEmpty(a)
	fmt.Printf("b is %v\n", b)
	fmt.Printf("&a[0] == &b[0] is %t\n", &a[0] == &b[0])

}
Output:

b == nil is false, len(b) is 0
b == nil is false, len(b) is 0
b is [a b c]
&a[0] == &b[0] is false

func ContainsFold added in v0.9.1

func ContainsFold(s, substr string) (ok bool)

ContainsFold reports whether s contains, ignoring letter case, substr.

Example
package main

import (
	"fmt"

	"github.com/AdguardTeam/golibs/stringutil"
)

func main() {
	if stringutil.ContainsFold("abc", "b") {
		fmt.Println("works with the same case")
	}

	if stringutil.ContainsFold("abc", "B") {
		fmt.Println("works with a different case")
	}

}
Output:

works with the same case
works with a different case

func FilterOut

func FilterOut(strs []string, f func(s string) (ok bool)) (filtered []string)

FilterOut returns a copy of strs with all strings for which f returned true removed.

Example
package main

import (
	"fmt"

	"github.com/AdguardTeam/golibs/stringutil"
)

func main() {
	strs := []string{
		"some text",
		"",
		"# comments",
	}

	// Remove all empty and comment lines.
	filtered := stringutil.FilterOut(strs, func(s string) (ok bool) {
		return len(s) == 0 || s[0] == '#'
	})

	fmt.Printf("%q\n", filtered)

}
Output:

["some text"]

func SplitTrimmed added in v0.8.4

func SplitTrimmed(str, sep string) (strs []string)

SplitTrimmed slices str into all substrings separated by sep and returns a slice of the trimmed substrings between those separators with empty strings skipped. If str has no such substrings, strs is an empty slice.

Example
package main

import (
	"fmt"

	"github.com/AdguardTeam/golibs/stringutil"
)

func main() {
	s := ""
	fmt.Printf("%q is split into %q\n", s, stringutil.SplitTrimmed(s, ","))

	s = "a, b  ,  , c"
	fmt.Printf("%q is split into %q\n", s, stringutil.SplitTrimmed(s, ","))

}
Output:

"" is split into []
"a, b  ,  , c" is split into ["a" "b" "c"]

func WriteToBuilder

func WriteToBuilder(b *strings.Builder, strs ...string)

WriteToBuilder is a convenient wrapper for strings.(*Builder).WriteString that deals with multiple strings and ignores errors, since they are guaranteed to be nil.

b must not be nil.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/AdguardTeam/golibs/stringutil"
)

func main() {
	b := &strings.Builder{}

	stringutil.WriteToBuilder(
		b,
		"a",
		"b",
		"c",
	)

	fmt.Println(b)

}
Output:

abc

Types

This section is empty.

Jump to

Keyboard shortcuts

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