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 ¶
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
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 ¶
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
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 ¶
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.