| 1 |
--- |
| 2 |
title: strings.ReplacePairs |
| 3 |
description: Returns a copy of a string with multiple replacements performed in a single pass, using a slice of old and new string pairs. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: string |
| 10 |
signatures: ['strings.ReplacePairs OLD NEW [OLD NEW ...] INPUT'] |
| 11 |
--- |
| 12 |
|
| 13 |
{{< new-in 0.158.0 />}} |
| 14 |
|
| 15 |
Use the `strings.ReplacePairs` function to perform multiple replacements on a string in a single operation. This approach is faster than sequentially calling the [`strings.Replace`][] function. |
| 16 |
|
| 17 |
Replacing strings sequentially requires multiple function calls and variable re-assignments. |
| 18 |
|
| 19 |
```go-html-template |
| 20 |
{{ $s := "aabbcc" }} |
| 21 |
{{ $s = strings.Replace $s "a" "x" }} |
| 22 |
{{ $s = strings.Replace $s "b" "y" }} |
| 23 |
{{ $s = strings.Replace $s "c" "z" }} |
| 24 |
{{ $s }} → xxyyzz |
| 25 |
``` |
| 26 |
|
| 27 |
Using `strings.ReplacePairs` produces the same result with fewer function calls in less time. |
| 28 |
|
| 29 |
```go-html-template |
| 30 |
{{ "aabbcc" | strings.ReplacePairs "a" "x" "b" "y" "c" "z" }} → xxyyzz |
| 31 |
``` |
| 32 |
|
| 33 |
Pairs may also be passed as a single slice: |
| 34 |
|
| 35 |
```go-html-template |
| 36 |
{{ $pairs := slice |
| 37 |
"a" "x" |
| 38 |
"b" "y" |
| 39 |
"c" "z" |
| 40 |
}} |
| 41 |
{{ "aabbcc" | strings.ReplacePairs $pairs }} → xxyyzz |
| 42 |
``` |
| 43 |
|
| 44 |
## Examples |
| 45 |
|
| 46 |
Observe that replacements are not applied recursively because the function scans the string only once. |
| 47 |
|
| 48 |
```go-html-template |
| 49 |
{{ $pairs := slice |
| 50 |
"a" "b" |
| 51 |
"b" "c" |
| 52 |
}} |
| 53 |
{{ "a" | strings.ReplacePairs $pairs }} → b |
| 54 |
``` |
| 55 |
|
| 56 |
Apply the first match when multiple old strings could match at the same position. |
| 57 |
|
| 58 |
```go-html-template |
| 59 |
{{ $pairs := slice |
| 60 |
"app" "pear" |
| 61 |
"apple" "orange" |
| 62 |
}} |
| 63 |
{{ "apple" | strings.ReplacePairs $pairs }} → pearle |
| 64 |
``` |
| 65 |
|
| 66 |
Delete specific strings by providing an empty string as the second value in a pair. |
| 67 |
|
| 68 |
```go-html-template |
| 69 |
{{ $pairs := slice "b" "" }} |
| 70 |
{{ "abc" | strings.ReplacePairs $pairs }} → ac |
| 71 |
``` |
| 72 |
|
| 73 |
## Edge cases |
| 74 |
|
| 75 |
The table below outlines how the function handles various input scenarios. |
| 76 |
|
| 77 |
Scenario|Result |
| 78 |
:--|:-- |
| 79 |
Fewer than two arguments|Error |
| 80 |
Odd number of slice elements|Error |
| 81 |
Empty slice|Returns the input string |
| 82 |
Empty input string|Returns an empty string |
| 83 |
Empty old string|Returns the input string [interleaved](g) with the new string |
| 84 |
|
| 85 |
## Performance |
| 86 |
|
| 87 |
While `strings.Replace` and `strings.ReplacePairs` can produce the same results, they handle data differently. Choosing the right one can noticeably reduce the time Hugo takes to build your project. |
| 88 |
|
| 89 |
### Single pass vs. multiple passes |
| 90 |
|
| 91 |
When using `strings.Replace`, Hugo must scan the text from start to finish to find a match. If you chain three replacements together, Hugo performs three separate passes over the entire string. |
| 92 |
|
| 93 |
The `strings.ReplacePairs` function is more efficient because it performs a single pass. Hugo looks through the text once and applies all replacements simultaneously. |
| 94 |
|
| 95 |
### Caching |
| 96 |
|
| 97 |
Unlike `strings.Replace`, which performs a direct substitution, `strings.ReplacePairs` requires an initialization step to prepare the single-pass replacement logic. To make this efficient, Hugo manages this logic using a cache: |
| 98 |
|
| 99 |
- During the initial call, Hugo initializes and stores the logic for that specific set of pairs. |
| 100 |
- During subsequent calls, Hugo retrieves the stored logic, skipping the initialization step and reducing the duration of the call. |
| 101 |
|
| 102 |
### Choosing the right function |
| 103 |
|
| 104 |
The efficiency of `strings.ReplacePairs` increases as the text gets longer or the number of pairs grows. Consider these scenarios when deciding which function to use: |
| 105 |
|
| 106 |
- For a single replacement on a short string like a title, `strings.Replace` is efficient. |
| 107 |
- For multiple replacements or long strings like a long-form article, `strings.ReplacePairs` is much faster. |
| 108 |
|
| 109 |
For a document with about 8000 characters, which is roughly the length of a long-form article, `strings.ReplacePairs` outperforms five sequential `strings.Replace` calls during the initial call. Once cached, it is the faster choice for almost any situation with two or more pairs. |
| 110 |
|
| 111 |
[`strings.Replace`]: /functions/strings/replace/ |