package strings
import "github.com/gohugoio/hugo/tpl/strings"
Package strings provides template functions for manipulating strings.
Index ¶
- type Namespace
- func New(d *deps.Deps) *Namespace
- func (ns *Namespace) Chomp(s any) (any, error)
- func (ns *Namespace) Contains(s, substr any) (bool, error)
- func (ns *Namespace) ContainsAny(s, chars any) (bool, error)
- func (ns *Namespace) ContainsNonSpace(s any) (bool, error)
- func (ns *Namespace) Count(substr, s any) (int, error)
- func (ns *Namespace) CountRunes(s any) (int, error)
- func (ns *Namespace) CountWords(s any) (int, error)
- func (ns *Namespace) Diff(oldname string, old any, newname string, new any) (string, error)
- func (ns *Namespace) FindRE(expr string, content any, limit ...any) ([]string, error)
- func (ns *Namespace) FindRESubmatch(expr string, content any, limit ...any) ([][]string, error)
- func (ns *Namespace) FirstUpper(s any) (string, error)
- func (ns *Namespace) HasPrefix(s, prefix any) (bool, error)
- func (ns *Namespace) HasSuffix(s, suffix any) (bool, error)
- func (ns *Namespace) Repeat(n, s any) (string, error)
- func (ns *Namespace) Replace(s, old, new any, limit ...any) (string, error)
- func (ns *Namespace) ReplaceRE(pattern, repl, s any, n ...any) (_ string, err error)
- func (ns *Namespace) RuneCount(s any) (int, error)
- func (ns *Namespace) SliceString(a any, startEnd ...any) (string, error)
- func (ns *Namespace) Split(a any, delimiter string) ([]string, error)
- func (ns *Namespace) Substr(a any, nums ...any) (string, error)
- func (ns *Namespace) Title(s any) (string, error)
- func (ns *Namespace) ToLower(s any) (string, error)
- func (ns *Namespace) ToUpper(s any) (string, error)
- func (ns *Namespace) Trim(s, cutset any) (string, error)
- func (ns *Namespace) TrimLeft(cutset, s any) (string, error)
- func (ns *Namespace) TrimPrefix(prefix, s any) (string, error)
- func (ns *Namespace) TrimRight(cutset, s any) (string, error)
- func (ns *Namespace) TrimSpace(s any) (string, error)
- func (ns *Namespace) TrimSuffix(suffix, s any) (string, error)
- func (ns *Namespace) Truncate(s any, options ...any) (template.HTML, error)
Types ¶
type Namespace ¶
type Namespace struct {
// contains filtered or unexported fields
}
Namespace provides template functions for the "strings" namespace. Most functions mimic the Go stdlib, but the order of the parameters may be different to ease their use in the Go template system.
func New ¶
New returns a new instance of the strings-namespaced template functions.
func (*Namespace) Chomp ¶
Chomp returns a copy of s with all trailing newline characters removed.
func (*Namespace) Contains ¶
Contains reports whether substr is in s.
func (*Namespace) ContainsAny ¶
ContainsAny reports whether any Unicode code points in chars are within s.
func (*Namespace) ContainsNonSpace ¶
ContainsNonSpace reports whether s contains any non-space characters as defined by Unicode's White Space property, <docsmeta>{"newIn": "0.111.0" }</docsmeta>
func (*Namespace) 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.
func (*Namespace) CountRunes ¶
CountRunes returns the number of runes in s, excluding whitespace.
func (*Namespace) CountWords ¶
CountWords returns the approximate word count in s.
func (*Namespace) Diff ¶
Diff returns an anchored diff of the two texts old and new in the “unified diff” format. If old and new are identical, Diff returns an empty string.
func (*Namespace) FindRE ¶
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.
func (*Namespace) FindRESubmatch ¶
FindRESubmatch returns a slice of all successive matches of the regular expression in content. Each element is a slice of strings holding the text of the leftmost match of the regular expression and the matches, if any, of its subexpressions.
By default all matches will be included. The number of matches can be limited with the optional limit parameter. A return value of nil indicates no match.
func (*Namespace) FirstUpper ¶
FirstUpper converts s making the first character upper case.
func (*Namespace) HasPrefix ¶
HasPrefix tests whether the input s begins with prefix.
func (*Namespace) HasSuffix ¶
HasSuffix tests whether the input s begins with suffix.
func (*Namespace) Repeat ¶
Repeat returns a new string consisting of n copies of the string s.
func (*Namespace) Replace ¶
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.
func (*Namespace) ReplaceRE ¶
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.
func (*Namespace) RuneCount ¶
RuneCount returns the number of runes in s.
func (*Namespace) SliceString ¶
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.
func (*Namespace) Split ¶
Split slices an input string into all substrings separated by delimiter.
func (*Namespace) Substr ¶
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.
func (*Namespace) Title ¶
Title returns a copy of the input s with all Unicode letters that begin words mapped to their title case.
func (*Namespace) ToLower ¶
ToLower returns a copy of the input s with all Unicode letters mapped to their lower case.
func (*Namespace) ToUpper ¶
ToUpper returns a copy of the input s with all Unicode letters mapped to their upper case.
func (*Namespace) Trim ¶
Trim returns converts the strings s removing all leading and trailing characters defined contained.
func (*Namespace) TrimLeft ¶
TrimLeft returns a slice of the string s with all leading characters contained in cutset removed.
func (*Namespace) TrimPrefix ¶
TrimPrefix returns s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.
func (*Namespace) TrimRight ¶
TrimRight returns a slice of the string s with all trailing characters contained in cutset removed.
func (*Namespace) TrimSpace ¶
TrimSpace returns the given string, removing leading and trailing whitespace as defined by Unicode.
func (*Namespace) TrimSuffix ¶
TrimSuffix returns s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.
func (*Namespace) Truncate ¶
Truncate truncates the string in s to the specified length.
Source Files ¶
init.go regexp.go strings.go truncate.go
- Version
- v0.144.2 (latest)
- Published
- Feb 19, 2025
- Platform
- linux/amd64
- Imports
- 17 packages
- Last checked
- 7 hours ago –
Tools for package owners.