Hugo essentially packed Go's strings package under the keyword strings. Therefore, the string manipulations are more or less similar to Go's string manipulations (They are NOT the same). However, for the benefit of the non-Go development viewers, I will list some useful functions here.
From time to time, you need to visit: https://godoc.org/github.com/gohugoio/hugo/tpl/strings to checkout any more available functions to use.
Useful for URL constructions or pattern recognition. To check whether a string has a prefix or suffix pattern, use the strings.HasPrefix and strings.HasSuffix functions:
{{- $stringValue := "Hugo" -}}{{ strings.HasPrefix $stringValue "H" }} # -> true{{ strings.HasSuffix $stringValue "go" }} # --> true{{ strings.HasPrefix $stringValue "go" }} # --> false{{ strings.HasSuffix $stringValue "H" }} # -> falseIn the case where you want to trim the prefix and suffix, the same strings.TrimPrefix and strings.TrimSuffix are the one good for the job. If the pattern is not found, the string remains as it is.
{{- $stringValue := "LetsGoForABeer" -}}{{ strings.TrimPrefix $stringValue "Lets" }} # -> GoForABeer{{ strings.TrimSuffix $stringValue "Beer" }} # --> LetsGoForA{{ strings.TrimPrefix $stringValue "Beer" }} # --> LetsGoForABeer{{ strings.TrimSuffix $stringValue "Lets" }} # -> LetsGoForABeerThese are conversion from Title, UPPERCASE, First upper, and lowercase.
Convert the string to Title Case.
{{- $stringValue := "lets go for a beer" -}}{{ strings.Title $stringValue }} # -> Lets Go For A BeerConvert the string to UPPERCASE.
{{- $stringValue := "lets go for a beer" -}}{{ strings.ToUpper $stringValue }} # -> LETS GO FOR A BEERCovert the string to lowercase.
{{- $stringValue := "LETS GO FOR A BEER" -}}{{ strings.ToLower $stringValue }} # -> lets go for a beerCovert only the first character to Uppercase.
{{- $stringValue := "lets go for a beer" -}}{{ strings.FirstUpper $stringValue }} # -> Lets go for a beerTo search sub-string.
{{- $stringValue := "lets go for a beer" -}}{{ strings.Contains $stringValue "beer" }} # -> trueFor lesser restriction,
{{- $stringValue := "lets go for a beer" -}}{{ strings.ContainsAny $stringValue "beer" }} # -> trueReplace all occurrence with given string.
{{- $stringValue := "lets go for a beer and another beer" -}}{{ strings.Replace $stringValue "whiskey" }} # -> lets go for a whiskey and another whiskey{{- $stringValue := "lets go for a beer" -}}{{- $start := 2 -}}{{- $end := 5 -}}{{ strings.SliceString $stringValue $start }} # -> "ts go for a beer"{{ strings.SliceString $stringValue $start $end }} # -> "ts g"{{- $stringValue := "lets go for a beer" -}}{{- $delimiter := " " -}}{{ strings.Split $stringValue $delimiter }} # -> ["lets", "go", "for", "a", "beer"]{{- $stringValue := "lets go for a beer" -}}{{ strings.CountRunes $stringValue }} # -> 14 (w/o whitespace){{ strings.RunesCount $stringValue }} # -> 18 (total runes){{- $stringValue := "lets go for a beer" -}}{{ strings.CountWords $stringValue }} # -> 5That's all about string manipulations.