strings
strings.Chomp
Chomp returns a copy of s
with all trailing newline characters removed.
Aliases: chomp
Examples
{{chomp "<p>Blockhead</p>\n" | safeHTML }}
<p>Blockhead</p>
strings.Contains
Contains reports whether substr is in s
.
Examples
{{ strings.Contains "abc" "b" }}
true
{{ strings.Contains "abc" "d" }}
false
strings.ContainsAny
ContainsAny reports whether any Unicode code points in chars are within s
.
Examples
{{ strings.ContainsAny "abc" "bcd" }}
true
{{ strings.ContainsAny "abc" "def" }}
false
strings.Count
Count counts the number of non-overlapping instances of substr
in s.
If substr
is an empty string, Count returns 1 + the number of Unicode code points in s.
Examples
{{"aabab" | strings.Count "a" }}
3
strings.CountRunes
CountRunes returns the number of runes in s
, excluding whitespace.
Aliases: countrunes
strings.CountWords
CountWords returns the approximate word count in s
.
Aliases: countwords
strings.FindRE
(expr string, content any, limit …any) → []string
FindRE returns a list of strings that match the regular expression. By default all matches will be included. The number of matches can be limited with an optional third parameter.
Aliases: findRE
Examples
{{ findRE "[G|g]o" "Hugo is a static side generator written in Go." "1" }}
[go]
strings.FirstUpper
FirstUpper returns a string with the first character as upper case.
Examples
{{ "hugo rocks!" | strings.FirstUpper }}
Hugo rocks!
strings.HasPrefix
HasPrefix tests whether the input s
begins with prefix.
Aliases: hasPrefix
Examples
{{ hasPrefix "Hugo" "Hu" }}
true
{{ hasPrefix "Hugo" "Fu" }}
false
strings.HasSuffix
HasSuffix tests whether the input s
begins with suffix.
strings.Repeat
Repeat returns a new string consisting of count copies of the string s.
Examples
{{ "yo" | strings.Repeat 4 }}
yoyoyoyo
strings.Replace
(s any, limit …any) → string
Replace returns a copy of the string s
with all occurrences of old replaced
with new. The number of replacements can be limited with an optional fourth
parameter.
Aliases: replace
Examples
{{ replace "Batman and Robin" "Robin" "Catwoman" }}
Batman and Catwoman
{{ replace "aabbaabb" "a" "z" 2 }}
zzbbaabb
strings.ReplaceRE
(pattern any, n …any) → string
ReplaceRE returns a copy of s, replacing all matches of the regular
expression pattern
with the replacement text repl. The number of replacements
can be limited with an optional fourth parameter.
Aliases: replaceRE
Examples
{{ replaceRE "a+b" "X" "aabbaabbab" }}
XbXbX
{{ replaceRE "a+b" "X" "aabbaabbab" 1 }}
Xbaabbab
strings.RuneCount
RuneCount returns the number of runes in s
.
strings.SliceString
(a any, startEnd …any) → string
SliceString slices a
string by specifying a
half-open range with
two indices, start and end. 1 and 4 creates a
slice including elements 1 through 3.
The end index can be omitted, it defaults to the string’s length.
Aliases: slicestr
Examples
{{slicestr "BatMan" 0 3}}
Bat
{{slicestr "BatMan" 3}}
Man
strings.Split
(a any, delimiter string) → []string
Split slices an input string into all substrings separated by delimiter
.
Aliases: split
strings.Substr
(a any, nums …any) → string
Substr extracts parts of a
string, beginning at the character at the specified
position, and returns the specified number of characters.
It normally takes two parameters: start and length. It can also take one parameter: start, i.e. length is omitted, in which case the substring starting from start until the end of the string will be returned.
To extract characters from the end of the string, use a
negative start number.
In addition, borrowing from the extended behavior described at http://php.net/substr, if length is given and is negative, then that many characters will be omitted from the end of string.
Aliases: substr
Examples
{{substr "BatMan" 0 -3}}
Bat
{{substr "BatMan" 3 3}}
Man
strings.Title
Title returns a copy of the input s
with all Unicode letters that begin words
mapped to their title case.
Aliases: title
Examples
{{title "Bat man"}}
Bat Man
{{title "somewhere over the rainbow"}}
Somewhere Over the Rainbow
strings.ToLower
ToLower returns a copy of the input s
with all Unicode letters mapped to their
lower case.
Aliases: lower
Examples
{{lower "BatMan"}}
batman
strings.ToUpper
ToUpper returns a copy of the input s
with all Unicode letters mapped to their
upper case.
Aliases: upper
Examples
{{upper "BatMan"}}
BATMAN
strings.Trim
Trim returns a string with all leading and trailing characters defined contained in cutset removed.
Aliases: trim
Examples
{{ trim "++Batman--" "+-" }}
Batman
strings.TrimLeft
TrimLeft returns a slice of the string s with all leading characters
contained in cutset
removed.
Examples
{{ "aabbaa" | strings.TrimLeft "a" }}
bbaa
strings.TrimPrefix
TrimPrefix returns s without the provided leading prefix
string. If s doesn’t
start with prefix
, s is returned unchanged.
Examples
{{ "aabbaa" | strings.TrimPrefix "a" }}
abbaa
{{ "aabbaa" | strings.TrimPrefix "aa" }}
bbaa
strings.TrimRight
TrimRight returns a slice of the string s with all trailing characters
contained in cutset
removed.
Examples
{{ "aabbaa" | strings.TrimRight "a" }}
aabb
strings.TrimSuffix
TrimSuffix returns s without the provided trailing suffix
string. If s
doesn’t end with suffix
, s is returned unchanged.
Examples
{{ "aabbaa" | strings.TrimSuffix "a" }}
aabba
{{ "aabbaa" | strings.TrimSuffix "aa" }}
aabb
strings.Truncate
(a any, options …any) → template.HTML
Truncate truncates a
given string to the specified length.
Aliases: truncate
Examples
{{ "this is a very long text" | truncate 10 " ..." }}
this is a ...
{{ "With [Markdown](/markdown) inside." | markdownify | truncate 14 }}
With <a href="/markdown">Markdown …</a>