strings
String manipulation functions from the Go standard library.
Note that, unlike in Go, many of these functions are also available as methods on string objects. See the related documentation here. That said, using these functions is handy within pipe expressions.
Functions
compare
compare(s1, s2 string) int
Compares two strings lexicographically. Returns -1 if s1 < s2, 0 if s1 == s2, and 1 if s1 > s2.
>>> strings.compare("abc", "abc")
0
>>> strings.compare("abc", "abd")
-1
contains
contains(s, substr string) bool
Returns true if the string s contains substr.
>>> strings.contains("abc", "b")
true
>>> strings.contains("abc", "d")
false
>>> "abc" | strings.contains("b")
true
count
count(s, substr string) int
Returns the number of non-overlapping instances of substr in s.
>>> strings.count("abc", "b")
1
>>> strings.count("ababab", "ab")
3
fields
fields(s string) []string
Splits the string s around each instance of one or more consecutive white space characters, returning a slice of substrings or any empty slice if s contains only white space.
>>> strings.fields("a b c")
["a", "b", "c"]
>>> strings.fields("")
[]
has_prefix
has_prefix(s, prefix string) bool
Returns true if the string s begins with prefix.
>>> strings.has_prefix("abc", "a")
true
has_suffix
has_suffix(s, suffix string) bool
Returns true if the string s ends with suffix.
>>> strings.has_suffix("abc", "c")
true
index
index(s, substr string) int
Returns the index of the first instance of substr in s, or -1 if substr is not present in s.
>>> strings.index("abc", "b")
1
>>> strings.index("abc", "d")
-1
join
join(a []string, sep string) string
Concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string.
>>> strings.join(["a", "b", "c"], ", ")
"a, b, c"
last_index
last_index(s, substr string) int
Returns the index of the last instance of substr in s, or -1 if substr is not present in s.
>>> strings.last_index("abc", "b")
1
>>> strings.last_index("abc", "d")
-1
repeat
repeat(s string, count int) string
Repeat returns a new string consisting of count
copies of the string s
.
>>> strings.repeat("na", 5) + " batman"
"nanananana batman"
replace_all
replace_all(s, old, new string) string
Returns a copy of the string s with all non-overlapping instances of old replaced by new.
>>> strings.replace_all("oink oink oink", "oink", "moo")
"moo moo moo"
split
split(s, sep string) []string
Splits the string s around each instance of sep, returning a slice of substrings or any empty slice if s does not contain sep.
>>> strings.split("a,b,c", ",")
["a", "b", "c"]
to_lower
to_lower(s string) string
Returns a copy of the string s with all Unicode letters mapped to their lower case.
>>> strings.to_lower("HELLO")
"hello"
to_upper
to_upper(s string) string
Returns a copy of the string s with all Unicode letters mapped to their upper case.
>>> strings.to_upper("hello")
"HELLO"
trim_prefix
trim_prefix(s, prefix string) string
Returns s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.
>>> strings.trim_prefix("foo", "f")
"oo"
>>> strings.trim_prefix("foo", "b")
"foo"
trim_space
trim_space(s string) string
Returns a slice of the string s, with all leading and trailing white space removed, as defined by Unicode.
>>> strings.trim_space(" hello ")
"hello"
trim_suffix
trim_suffix(s, suffix string) string
Returns s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.
>>> strings.trim_suffix("foo", "o")
"f"
trim
trim(s, cutset string) string
Returns a slice of the string s, with all leading and trailing Unicode code points contained in cutset removed.
>>> strings.trim("¡¡¡Hello, Gophers!!!", "!¡")
"Hello, Gophers"