HUGO
Menu
GitHub 90272 stars Mastodon

strings.Replace

Returns the given string, replacing all occurrences of OLD with NEW.
Syntax
strings.Replace STRING OLD NEW [LIMIT]
Returns
string
Alias
replace

Usage

The strings.Replace function returns the given string, replacing all occurrences of OLD with NEW. To limit the number of replacements, pass the optional LIMIT argument.

Examples

{{ $s := "Batman and Robin" }}
{{ strings.Replace $s "Robin" "Catwoman" }} → Batman and Catwoman

Limiting the number of replacements:

{{ strings.Replace "aabbaabb" "a" "z" 2 }} → zzbbaabb