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) intCompares 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")
-1contains
contains(s, substr string) boolReturns true if the string s contains substr.
>>> strings.contains("abc", "b")
true
>>> strings.contains("abc", "d")
false
>>> "abc" | strings.contains("b")
truecount
count(s, substr string) intReturns the number of non-overlapping instances of substr in s.
>>> strings.count("abc", "b")
1
>>> strings.count("ababab", "ab")
3fields
fields(s string) []stringSplits 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) boolReturns true if the string s begins with prefix.
>>> strings.has_prefix("abc", "a")
truehas_suffix
has_suffix(s, suffix string) boolReturns true if the string s ends with suffix.
>>> strings.has_suffix("abc", "c")
trueindex
index(s, substr string) intReturns 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")
-1join
join(a []string, sep string) stringConcatenates 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) intReturns 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")
-1repeat
repeat(s string, count int) stringRepeat 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) stringReturns 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) []stringSplits 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) stringReturns 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) stringReturns 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) stringReturns 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) stringReturns 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) stringReturns 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) stringReturns a slice of the string s, with all leading and trailing Unicode code points contained in cutset removed.
>>> strings.trim("¡¡¡Hello, Gophers!!!", "!¡")
"Hello, Gophers"