Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท strings and strconv

Go strings.Replacer type

Reference for Go strings.Replacer and NewReplacer: apply many replacements in one pass, learn the matching order, and write results straight to an io.Writer.

A strings.Replacer performs several string replacements in a single pass over the input. You build it once with strings.NewReplacer, giving it pairs of old and new strings, and then call Replace as often as you like. Because the pass is single, the replaced text is never scanned again: replacing a with b and b with a swaps the letters instead of turning everything into one of them.

A replacer is the right tool for HTML-style escaping, template placeholders and any "translate these tokens" job.

Go
r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
fmt.Println(r.Replace("<b>hi</b>"))

Try it

Go Escaping and swapping

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Placeholders, order and writers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func NewReplacer(oldnew ...string) *Replacer
func (r *Replacer) Replace(s string) string
func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)
Name Description
oldnew Alternating old and new strings: old1, new1, old2, new2, ....
s The text to transform.
w Destination for WriteString.

Returns: NewReplacer returns a ready *Replacer. Replace returns the new string. WriteString returns the bytes written and any error from w.

Panics: NewReplacer panics if you pass an odd number of arguments.

Usage notes

  • Order decides ties. At each position the old strings are tried in argument order, so list longer patterns before their prefixes ("aa" before "a"), as the second example shows.
  • Matches never overlap and are never rescanned. That is why a swap works in one call, which two chained strings.ReplaceAll calls cannot do.
  • An empty old string matches at every position (including before each character and at the end), which inserts the new text between all characters. This is rarely what you want.
  • Safe for concurrent use. Build one replacer at package level and share it between goroutines.
  • Fast paths. When every old string is a single byte, Go builds a byte-lookup table, and other cases use a prefix tree, so a large pair list is cheaper than looping over ReplaceAll.
  • For a single pair, use strings.ReplaceAll. A replacer only pays off with several pairs or repeated use.
  • Not a template engine. For conditionals and loops use text/template; for HTML output prefer html/template or html.EscapeString.